Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1041:5fcc39857c5c
prev1038:f220d18c0615
next1072:d82e04e6d497
author nkeynes
date Fri Jun 26 05:47:04 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Refactor path operations into lxpaths.[ch]
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@450
    28
#include "dream.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
bhaal22@643
    35
extern struct lxdream_config_entry alsa_config[];
nkeynes@1015
    36
extern struct lxdream_config_entry hotkeys_config[];
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@450
    43
static struct lxdream_config_entry global_config[] =
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@736
    54
        { NULL, CONFIG_TYPE_NONE }};
nkeynes@450
    55
nkeynes@450
    56
static struct lxdream_config_entry serial_config[] =
nkeynes@736
    57
       {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
nkeynes@736
    58
        { NULL, CONFIG_TYPE_NONE }};
nkeynes@450
    59
nkeynes@1024
    60
struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] = 
nkeynes@736
    61
       {{ "global", global_config },
nkeynes@736
    62
        { "controllers", NULL },
nkeynes@1015
    63
        { "hotkeys", hotkeys_config },
nkeynes@736
    64
        { "serial", serial_config },
nkeynes@736
    65
        { NULL, CONFIG_TYPE_NONE }};
nkeynes@450
    66
nkeynes@450
    67
static gchar *lxdream_config_load_filename = NULL;
nkeynes@450
    68
static gchar *lxdream_config_save_filename = NULL;
nkeynes@450
    69
nkeynes@1024
    70
void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
nkeynes@1024
    71
{
nkeynes@1024
    72
    int i;
nkeynes@1024
    73
    for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
nkeynes@1024
    74
        if( lxdream_config_root[i].key == NULL ) {
nkeynes@1024
    75
            lxdream_config_root[i].key = key;
nkeynes@1024
    76
            lxdream_config_root[i].params = group;
nkeynes@1024
    77
            lxdream_config_root[i+1].key = NULL;
nkeynes@1024
    78
            lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
nkeynes@1024
    79
            return;
nkeynes@1024
    80
        }
nkeynes@1024
    81
    }
nkeynes@1024
    82
    ERROR( "Unable to register config group '%s': Too many configuration groups", key );
nkeynes@1024
    83
}
nkeynes@1024
    84
nkeynes@450
    85
gboolean lxdream_find_config()
nkeynes@450
    86
{
nkeynes@480
    87
    gboolean result = TRUE;
nkeynes@450
    88
    char *home = getenv("HOME");
nkeynes@450
    89
    if( lxdream_config_save_filename == NULL ) {
nkeynes@1038
    90
        /* For compatibility, look for the old ~/.lxdreamrc first. Use it if 
nkeynes@1038
    91
         * found, otherwise use the new ~/.lxdream/lxdreamrc.
nkeynes@1038
    92
         */
nkeynes@736
    93
        lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
nkeynes@1038
    94
        if( access(lxdream_config_save_filename, R_OK) != 0 ) {
nkeynes@1038
    95
            g_free(lxdream_config_save_filename);
nkeynes@1038
    96
            const char *user_path = get_user_data_path();
nkeynes@1038
    97
            lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
nkeynes@1038
    98
        }
nkeynes@450
    99
    }
nkeynes@450
   100
    if( lxdream_config_load_filename == NULL ) {
nkeynes@866
   101
        char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
nkeynes@736
   102
        if( access(lxdream_config_save_filename, R_OK) == 0 ) {
nkeynes@736
   103
            lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
nkeynes@866
   104
            g_free(sysconfig);
nkeynes@866
   105
        } else if( access( sysconfig, R_OK ) == 0 ) {
nkeynes@866
   106
            lxdream_config_load_filename = sysconfig;
nkeynes@736
   107
        } else {
nkeynes@736
   108
            lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
nkeynes@866
   109
            g_free(sysconfig);
nkeynes@736
   110
            result = FALSE;
nkeynes@736
   111
        }	
nkeynes@480
   112
    }
nkeynes@480
   113
    return result;
nkeynes@450
   114
}
nkeynes@450
   115
nkeynes@450
   116
void lxdream_set_config_filename( const gchar *filename )
nkeynes@450
   117
{
nkeynes@450
   118
    if( lxdream_config_load_filename != NULL ) {
nkeynes@736
   119
        g_free(lxdream_config_load_filename);
nkeynes@450
   120
    }
nkeynes@450
   121
    lxdream_config_load_filename = g_strdup(filename);
nkeynes@450
   122
    if( lxdream_config_save_filename != NULL ) {
nkeynes@736
   123
        g_free(lxdream_config_save_filename);
nkeynes@450
   124
    }
nkeynes@450
   125
    lxdream_config_save_filename = g_strdup(filename);
nkeynes@450
   126
}
nkeynes@450
   127
nkeynes@450
   128
void lxdream_set_default_config( )
nkeynes@450
   129
{
nkeynes@1038
   130
    /* Construct platform dependent defaults */
nkeynes@1038
   131
    const gchar *user_path = get_user_data_path();
nkeynes@1038
   132
    global_config[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path ); 
nkeynes@1038
   133
    global_config[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path ); 
nkeynes@1038
   134
    global_config[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path ); 
nkeynes@1038
   135
    global_config[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path ); 
nkeynes@1038
   136
    global_config[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path ); 
nkeynes@1038
   137
    
nkeynes@1038
   138
    /* Copy defaults into main values */
nkeynes@450
   139
    struct lxdream_config_group *group = lxdream_config_root;
nkeynes@450
   140
    while( group->key != NULL ) {
nkeynes@736
   141
        struct lxdream_config_entry *param = group->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@736
   152
        group++;
nkeynes@450
   153
    }
nkeynes@450
   154
    maple_detach_all();
nkeynes@450
   155
}
nkeynes@450
   156
nkeynes@1036
   157
const gchar *lxdream_get_global_config_value( int key )
nkeynes@450
   158
{
nkeynes@450
   159
    return global_config[key].value;
nkeynes@450
   160
}
nkeynes@450
   161
nkeynes@1034
   162
GList *lxdream_get_global_config_list_value( int key )
nkeynes@1034
   163
{
nkeynes@1034
   164
    GList *result = NULL;
nkeynes@1036
   165
    const gchar *str = lxdream_get_global_config_value( key );
nkeynes@1034
   166
    if( str != NULL ) {
nkeynes@1034
   167
        gchar **strv = g_strsplit(str, ":",0);
nkeynes@1034
   168
        int i;
nkeynes@1034
   169
        for( i=0; strv[i] != NULL; i++ ) {
nkeynes@1034
   170
            result = g_list_append( result, g_strdup(strv[i]) );
nkeynes@1034
   171
        }
nkeynes@1034
   172
        g_strfreev(strv);
nkeynes@1034
   173
    }
nkeynes@1034
   174
    return result;
nkeynes@1034
   175
}
nkeynes@1034
   176
nkeynes@1034
   177
void lxdream_set_global_config_list_value( int key, const GList *list )
nkeynes@1034
   178
{
nkeynes@1034
   179
    if( list == NULL ) {
nkeynes@1034
   180
        lxdream_set_global_config_value( key, NULL );
nkeynes@1034
   181
    } else {
nkeynes@1036
   182
        const GList *ptr;
nkeynes@1034
   183
        int size = 0;
nkeynes@1034
   184
        
nkeynes@1034
   185
        for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@1034
   186
            size += strlen( (gchar *)ptr->data ) + 1;
nkeynes@1034
   187
        }
nkeynes@1034
   188
        char buf[size];
nkeynes@1034
   189
        strcpy( buf, (gchar *)list->data );
nkeynes@1034
   190
        for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@1034
   191
            strcat( buf, ":" );
nkeynes@1034
   192
            strcat( buf, (gchar *)ptr->data );
nkeynes@1034
   193
        }
nkeynes@1034
   194
        lxdream_set_global_config_value( key, buf );
nkeynes@1034
   195
    }
nkeynes@1034
   196
}
nkeynes@1034
   197
nkeynes@1036
   198
gchar *lxdream_get_global_config_path_value( int key )
nkeynes@1036
   199
{
nkeynes@1036
   200
    const gchar *str = lxdream_get_global_config_value(key);
nkeynes@1036
   201
    if( str == NULL ) {
nkeynes@1036
   202
        return NULL;
nkeynes@1036
   203
    } else {
nkeynes@1036
   204
        return get_expanded_path(str);
nkeynes@1036
   205
    }
nkeynes@1036
   206
}
nkeynes@1036
   207
nkeynes@1036
   208
const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
nkeynes@1036
   209
{
nkeynes@1036
   210
    gchar *temp = get_escaped_path(value);
nkeynes@1036
   211
    lxdream_set_global_config_value(key,temp);
nkeynes@1036
   212
    g_free(temp);
nkeynes@1036
   213
    return lxdream_get_global_config_value(key);
nkeynes@1036
   214
}
nkeynes@1036
   215
nkeynes@460
   216
void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
nkeynes@450
   217
{
nkeynes@475
   218
    if( param->value != value ) {
nkeynes@736
   219
        if( param->value != param->default_value && param->value != NULL ) {
nkeynes@736
   220
            free( param->value );
nkeynes@736
   221
        }
nkeynes@736
   222
        param->value = g_strdup(value);
nkeynes@450
   223
    }
nkeynes@450
   224
}
nkeynes@450
   225
nkeynes@460
   226
void lxdream_set_global_config_value( int key, const gchar *value )
nkeynes@460
   227
{
nkeynes@460
   228
    lxdream_set_config_value(&global_config[key], value);
nkeynes@460
   229
}
nkeynes@460
   230
nkeynes@1036
   231
const struct lxdream_config_entry * lxdream_get_global_config_entry( int key )
nkeynes@724
   232
{
nkeynes@724
   233
    return &global_config[key];
nkeynes@724
   234
}
nkeynes@724
   235
nkeynes@458
   236
gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
nkeynes@458
   237
{
nkeynes@458
   238
    int i;
nkeynes@458
   239
    for( i=0; group->params[i].key != NULL; i++ ) {
nkeynes@736
   240
        if( strcasecmp( group->params[i].key, key ) == 0 ) {
nkeynes@736
   241
            lxdream_set_config_value( &group->params[i], value );
nkeynes@736
   242
            return TRUE;
nkeynes@736
   243
        }
nkeynes@458
   244
    }
nkeynes@458
   245
    return FALSE;
nkeynes@458
   246
}
nkeynes@458
   247
nkeynes@460
   248
void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
nkeynes@460
   249
{
nkeynes@460
   250
    int i;
nkeynes@460
   251
    for( i=0; src[i].key != NULL; i++ ) {
nkeynes@736
   252
        lxdream_set_config_value( &dest[i], src[i].value );
nkeynes@460
   253
    }
nkeynes@460
   254
}
nkeynes@460
   255
nkeynes@450
   256
gboolean lxdream_load_config( )
nkeynes@450
   257
{
nkeynes@450
   258
    if( lxdream_config_load_filename == NULL ) {
nkeynes@736
   259
        lxdream_find_config();
nkeynes@450
   260
    }
nkeynes@450
   261
    return lxdream_load_config_file(lxdream_config_load_filename);
nkeynes@450
   262
}
nkeynes@450
   263
nkeynes@450
   264
gboolean lxdream_save_config( )
nkeynes@450
   265
{
nkeynes@450
   266
    if( lxdream_config_save_filename == NULL ) {
nkeynes@736
   267
        lxdream_find_config();
nkeynes@450
   268
    }
nkeynes@450
   269
    return lxdream_save_config_file(lxdream_config_save_filename);
nkeynes@450
   270
}
nkeynes@450
   271
nkeynes@450
   272
gboolean lxdream_load_config_file( const gchar *filename )
nkeynes@450
   273
{
nkeynes@450
   274
    FILE *f;
nkeynes@450
   275
    gboolean result;
nkeynes@450
   276
nkeynes@450
   277
    if( access(filename, F_OK) != 0 ) {
nkeynes@736
   278
        INFO( "Configuration file '%s' does not exist, creating from defaults" );
nkeynes@736
   279
        lxdream_set_default_config();
nkeynes@736
   280
        lxdream_save_config();
nkeynes@450
   281
    }
nkeynes@450
   282
nkeynes@450
   283
    f = fopen(filename, "ro");
nkeynes@450
   284
    if( f == NULL ) {
nkeynes@736
   285
        ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
nkeynes@736
   286
        lxdream_set_default_config();
nkeynes@736
   287
        return FALSE;
nkeynes@450
   288
    }
nkeynes@450
   289
nkeynes@450
   290
    result = lxdream_load_config_stream( f );
nkeynes@450
   291
    fclose(f);
nkeynes@450
   292
    return result;
nkeynes@450
   293
}
nkeynes@450
   294
nkeynes@450
   295
gboolean lxdream_load_config_stream( FILE *f )
nkeynes@450
   296
{
nkeynes@450
   297
nkeynes@450
   298
    char buf[512];
nkeynes@450
   299
    int maple_device = -1, maple_subdevice = -1;
nkeynes@450
   300
    struct lxdream_config_group devgroup;
nkeynes@450
   301
    struct lxdream_config_group *group = NULL;
nkeynes@450
   302
    maple_device_t device = NULL;
nkeynes@450
   303
    lxdream_set_default_config();
nkeynes@450
   304
nkeynes@450
   305
    while( fgets( buf, sizeof(buf), f ) != NULL ) {
nkeynes@736
   306
        g_strstrip(buf);
nkeynes@736
   307
        if( buf[0] == '#' )
nkeynes@736
   308
            continue;
nkeynes@736
   309
        if( *buf == '[' ) {
nkeynes@736
   310
            char *p = strchr(buf, ']');
nkeynes@736
   311
            if( p != NULL ) {
nkeynes@736
   312
                struct lxdream_config_group *tmp_group;
nkeynes@736
   313
                maple_device = maple_subdevice = -1;
nkeynes@736
   314
                *p = '\0';
nkeynes@736
   315
                g_strstrip(buf+1);
nkeynes@736
   316
                tmp_group = &lxdream_config_root[0];
nkeynes@736
   317
                while( tmp_group->key != NULL ) {
nkeynes@736
   318
                    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
nkeynes@736
   319
                        group = tmp_group;
nkeynes@736
   320
                        break;
nkeynes@736
   321
                    }
nkeynes@736
   322
                    tmp_group++;
nkeynes@736
   323
                }
nkeynes@736
   324
            }
nkeynes@736
   325
        } else if( group != NULL ) {
nkeynes@736
   326
            char *value = strchr( buf, '=' );
nkeynes@736
   327
            if( value != NULL ) {
nkeynes@736
   328
                struct lxdream_config_entry *param = group->params;
nkeynes@736
   329
                *value = '\0';
nkeynes@736
   330
                value++;
nkeynes@736
   331
                g_strstrip(buf);
nkeynes@736
   332
                g_strstrip(value);
nkeynes@736
   333
                if( strcmp(group->key,"controllers") == 0  ) {
nkeynes@736
   334
                    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
nkeynes@736
   335
                        maple_device = strtoul( buf+7, NULL, 0 );
nkeynes@736
   336
                        if( maple_device < 0 || maple_device > 3 ) {
nkeynes@736
   337
                            ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
nkeynes@736
   338
                            continue;
nkeynes@736
   339
                        }
nkeynes@736
   340
                        maple_subdevice = 0;
nkeynes@736
   341
                        device = maple_new_device( value );
nkeynes@736
   342
                        if( device == NULL ) {
nkeynes@736
   343
                            ERROR( "Unrecognized device '%s'", value );
nkeynes@736
   344
                        } else {
nkeynes@736
   345
                            devgroup.key = "controllers";
nkeynes@736
   346
                            devgroup.params = maple_get_device_config(device);
nkeynes@736
   347
                            maple_attach_device( device, maple_device, maple_subdevice );
nkeynes@736
   348
                            group = &devgroup;
nkeynes@736
   349
                        }
nkeynes@736
   350
                        continue;
nkeynes@736
   351
                    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
nkeynes@736
   352
                        maple_subdevice = strtoul( buf+10, NULL, 0 );
nkeynes@736
   353
                        if( maple_device == -1 ) {
nkeynes@736
   354
                            ERROR( "Subdevice not allowed without primary device" );
nkeynes@736
   355
                        } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
nkeynes@736
   356
                            ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
nkeynes@736
   357
                        } else if( (device = maple_new_device(value)) == NULL ) {
nkeynes@736
   358
                            ERROR( "Unrecognized subdevice '%s'", value );
nkeynes@736
   359
                        } else {
nkeynes@736
   360
                            devgroup.key = "controllers";
nkeynes@736
   361
                            devgroup.params = maple_get_device_config(device);
nkeynes@736
   362
                            maple_attach_device( device, maple_device, maple_subdevice );
nkeynes@736
   363
                            group = &devgroup;
nkeynes@736
   364
                        }
nkeynes@736
   365
                        continue;
nkeynes@736
   366
                    }
nkeynes@736
   367
                }
nkeynes@736
   368
                while( param->key != NULL ) {
nkeynes@736
   369
                    if( strcasecmp( param->key, buf ) == 0 ) {
nkeynes@736
   370
                        param->value = g_strdup(value);
nkeynes@736
   371
                        break;
nkeynes@736
   372
                    }
nkeynes@736
   373
                    param++;
nkeynes@736
   374
                }
nkeynes@736
   375
            }
nkeynes@736
   376
        }
nkeynes@450
   377
    }
nkeynes@450
   378
    return TRUE;
nkeynes@450
   379
}
nkeynes@450
   380
nkeynes@450
   381
gboolean lxdream_save_config_file( const gchar *filename )
nkeynes@450
   382
{
nkeynes@450
   383
    FILE *f = fopen(filename, "wo");
nkeynes@450
   384
    gboolean result;
nkeynes@450
   385
    if( f == NULL ) {
nkeynes@736
   386
        ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
nkeynes@736
   387
        return FALSE;
nkeynes@450
   388
    }
nkeynes@450
   389
    result = lxdream_save_config_stream(f);
nkeynes@450
   390
    fclose(f);
nkeynes@450
   391
    return TRUE;
nkeynes@450
   392
}    
nkeynes@450
   393
nkeynes@450
   394
gboolean lxdream_save_config_stream( FILE *f )
nkeynes@450
   395
{
nkeynes@450
   396
    struct lxdream_config_group *group = &lxdream_config_root[0];
nkeynes@736
   397
nkeynes@450
   398
    while( group->key != NULL ) {
nkeynes@736
   399
        struct lxdream_config_entry *entry = group->params;
nkeynes@736
   400
        fprintf( f, "[%s]\n", group->key );
nkeynes@736
   401
nkeynes@736
   402
        if( entry != NULL ) {
nkeynes@736
   403
            while( entry->key != NULL ) {
nkeynes@736
   404
                if( entry->value != NULL ) {
nkeynes@736
   405
                    fprintf( f, "%s = %s\n", entry->key, entry->value );
nkeynes@736
   406
                }
nkeynes@736
   407
                entry++;
nkeynes@736
   408
            }
nkeynes@736
   409
        } else if( strcmp(group->key, "controllers") == 0 ) {
nkeynes@736
   410
            int i,j;
nkeynes@736
   411
            for( i=0; i<4; i++ ) {
nkeynes@736
   412
                for( j=0; j<6; j++ ) {
nkeynes@736
   413
                    maple_device_t dev = maple_get_device( i, j );
nkeynes@736
   414
                    if( dev != NULL ) {
nkeynes@736
   415
                        if( j == 0 )
nkeynes@736
   416
                            fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
nkeynes@736
   417
                        else 
nkeynes@736
   418
                            fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
nkeynes@736
   419
                        if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
nkeynes@736
   420
                            while( entry->key != NULL ) {
nkeynes@736
   421
                                if( entry->value != NULL ) {
nkeynes@736
   422
                                    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
nkeynes@736
   423
                                }
nkeynes@736
   424
                                entry++;
nkeynes@736
   425
                            }
nkeynes@736
   426
                        }
nkeynes@736
   427
                    }
nkeynes@736
   428
                }
nkeynes@736
   429
            }
nkeynes@736
   430
        }
nkeynes@736
   431
        fprintf( f, "\n" );
nkeynes@736
   432
        group++;
nkeynes@450
   433
    }
nkeynes@450
   434
    return TRUE;
nkeynes@450
   435
}
nkeynes@1038
   436
nkeynes@1038
   437
void lxdream_make_config_dir( )
nkeynes@1038
   438
{
nkeynes@1038
   439
    const char *user_path = get_user_data_path();
nkeynes@1038
   440
    struct stat st;
nkeynes@1038
   441
nkeynes@1038
   442
    if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
nkeynes@1038
   443
            (st.st_mode & S_IFDIR) != 0 ) {
nkeynes@1038
   444
        /* All good */
nkeynes@1038
   445
        return;
nkeynes@1038
   446
    }
nkeynes@1038
   447
    
nkeynes@1038
   448
    if( mkdir( user_path, 0777 ) != 0 ) {
nkeynes@1038
   449
        ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
nkeynes@1038
   450
        return;
nkeynes@1038
   451
    }
nkeynes@1038
   452
nkeynes@1038
   453
    char *vmupath = g_strdup_printf( "%s/vmu", user_path );
nkeynes@1038
   454
    mkdir( vmupath, 0777 );
nkeynes@1038
   455
    g_free( vmupath );
nkeynes@1038
   456
    
nkeynes@1038
   457
    char *savepath = g_strdup_printf( "%s/save", user_path );
nkeynes@1038
   458
    mkdir( savepath, 0777 );
nkeynes@1038
   459
    g_free( vmupath );
nkeynes@1038
   460
}
.