Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1034:7044e01148f0
prev1024:c67f2d61ab97
next1036:af7b0c5905dd
author nkeynes
date Wed Jun 24 02:41:12 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add initial VMU support
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@450
    26
#include "dream.h"
nkeynes@450
    27
#include "config.h"
nkeynes@450
    28
#include "maple/maple.h"
nkeynes@450
    29
nkeynes@1024
    30
#define MAX_ROOT_GROUPS 16
bhaal22@643
    31
bhaal22@643
    32
extern struct lxdream_config_entry alsa_config[];
nkeynes@1015
    33
extern struct lxdream_config_entry hotkeys_config[];
bhaal22@643
    34
nkeynes@450
    35
gboolean lxdream_load_config_file( const gchar *filename );
nkeynes@450
    36
gboolean lxdream_save_config_file( const gchar *filename );
nkeynes@450
    37
gboolean lxdream_load_config_stream( FILE *f );
nkeynes@450
    38
gboolean lxdream_save_config_stream( FILE *f );
nkeynes@450
    39
nkeynes@450
    40
static struct lxdream_config_entry global_config[] =
nkeynes@736
    41
       {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, "dcboot.rom" },
nkeynes@736
    42
        { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, "dcflash.rom" },
nkeynes@736
    43
        { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
nkeynes@736
    44
        { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, "save" },
nkeynes@1034
    45
        { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, "vmu" },
nkeynes@736
    46
        { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
nkeynes@736
    47
        { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
nkeynes@1034
    48
        { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
nkeynes@1034
    49
        { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
nkeynes@736
    50
        { NULL, CONFIG_TYPE_NONE }};
nkeynes@450
    51
nkeynes@450
    52
static struct lxdream_config_entry serial_config[] =
nkeynes@736
    53
       {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
nkeynes@736
    54
        { NULL, CONFIG_TYPE_NONE }};
nkeynes@450
    55
nkeynes@1024
    56
struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] = 
nkeynes@736
    57
       {{ "global", global_config },
nkeynes@736
    58
        { "controllers", NULL },
nkeynes@1015
    59
        { "hotkeys", hotkeys_config },
nkeynes@736
    60
        { "serial", serial_config },
nkeynes@736
    61
        { NULL, CONFIG_TYPE_NONE }};
nkeynes@450
    62
nkeynes@450
    63
static gchar *lxdream_config_load_filename = NULL;
nkeynes@450
    64
static gchar *lxdream_config_save_filename = NULL;
nkeynes@450
    65
nkeynes@1024
    66
void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
nkeynes@1024
    67
{
nkeynes@1024
    68
    int i;
nkeynes@1024
    69
    for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
nkeynes@1024
    70
        if( lxdream_config_root[i].key == NULL ) {
nkeynes@1024
    71
            lxdream_config_root[i].key = key;
nkeynes@1024
    72
            lxdream_config_root[i].params = group;
nkeynes@1024
    73
            lxdream_config_root[i+1].key = NULL;
nkeynes@1024
    74
            lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
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@736
    86
        lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
nkeynes@450
    87
    }
nkeynes@450
    88
    if( lxdream_config_load_filename == NULL ) {
nkeynes@866
    89
        char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
nkeynes@736
    90
        if( access(lxdream_config_save_filename, R_OK) == 0 ) {
nkeynes@736
    91
            lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
nkeynes@866
    92
            g_free(sysconfig);
nkeynes@866
    93
        } else if( access( sysconfig, R_OK ) == 0 ) {
nkeynes@866
    94
            lxdream_config_load_filename = sysconfig;
nkeynes@736
    95
        } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
nkeynes@736
    96
            lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
nkeynes@866
    97
            g_free(sysconfig);
nkeynes@736
    98
        } else {
nkeynes@736
    99
            lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
nkeynes@866
   100
            g_free(sysconfig);
nkeynes@736
   101
            result = FALSE;
nkeynes@736
   102
        }	
nkeynes@480
   103
    }
nkeynes@480
   104
    return result;
nkeynes@450
   105
}
nkeynes@450
   106
nkeynes@450
   107
void lxdream_set_config_filename( const gchar *filename )
nkeynes@450
   108
{
nkeynes@450
   109
    if( lxdream_config_load_filename != NULL ) {
nkeynes@736
   110
        g_free(lxdream_config_load_filename);
nkeynes@450
   111
    }
nkeynes@450
   112
    lxdream_config_load_filename = g_strdup(filename);
nkeynes@450
   113
    if( lxdream_config_save_filename != NULL ) {
nkeynes@736
   114
        g_free(lxdream_config_save_filename);
nkeynes@450
   115
    }
nkeynes@450
   116
    lxdream_config_save_filename = g_strdup(filename);
nkeynes@450
   117
}
nkeynes@450
   118
nkeynes@450
   119
void lxdream_set_default_config( )
nkeynes@450
   120
{
nkeynes@450
   121
    struct lxdream_config_group *group = lxdream_config_root;
nkeynes@450
   122
    while( group->key != NULL ) {
nkeynes@736
   123
        struct lxdream_config_entry *param = group->params;
nkeynes@736
   124
        if( param != NULL ) {
nkeynes@736
   125
            while( param->key != NULL ) {
nkeynes@736
   126
                if( param->value != param->default_value ) {
nkeynes@736
   127
                    if( param->value != NULL )
nkeynes@736
   128
                        free( param->value );
nkeynes@736
   129
                    param->value = (gchar *)param->default_value;
nkeynes@736
   130
                }
nkeynes@736
   131
                param++;
nkeynes@736
   132
            }
nkeynes@736
   133
        }
nkeynes@736
   134
        group++;
nkeynes@450
   135
    }
nkeynes@450
   136
    maple_detach_all();
nkeynes@450
   137
}
nkeynes@450
   138
nkeynes@450
   139
const gchar *lxdream_get_config_value( int key )
nkeynes@450
   140
{
nkeynes@450
   141
    return global_config[key].value;
nkeynes@450
   142
}
nkeynes@450
   143
nkeynes@1034
   144
GList *lxdream_get_global_config_list_value( int key )
nkeynes@1034
   145
{
nkeynes@1034
   146
    GList *result = NULL;
nkeynes@1034
   147
    const gchar *str = lxdream_get_config_value( key );
nkeynes@1034
   148
    if( str != NULL ) {
nkeynes@1034
   149
        gchar **strv = g_strsplit(str, ":",0);
nkeynes@1034
   150
        int i;
nkeynes@1034
   151
        for( i=0; strv[i] != NULL; i++ ) {
nkeynes@1034
   152
            result = g_list_append( result, g_strdup(strv[i]) );
nkeynes@1034
   153
        }
nkeynes@1034
   154
        g_strfreev(strv);
nkeynes@1034
   155
    }
nkeynes@1034
   156
    return result;
nkeynes@1034
   157
}
nkeynes@1034
   158
nkeynes@1034
   159
void lxdream_set_global_config_list_value( int key, const GList *list )
nkeynes@1034
   160
{
nkeynes@1034
   161
    if( list == NULL ) {
nkeynes@1034
   162
        lxdream_set_global_config_value( key, NULL );
nkeynes@1034
   163
    } else {
nkeynes@1034
   164
        GList *ptr;
nkeynes@1034
   165
        int size = 0;
nkeynes@1034
   166
        
nkeynes@1034
   167
        for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@1034
   168
            size += strlen( (gchar *)ptr->data ) + 1;
nkeynes@1034
   169
        }
nkeynes@1034
   170
        char buf[size];
nkeynes@1034
   171
        strcpy( buf, (gchar *)list->data );
nkeynes@1034
   172
        for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@1034
   173
            strcat( buf, ":" );
nkeynes@1034
   174
            strcat( buf, (gchar *)ptr->data );
nkeynes@1034
   175
        }
nkeynes@1034
   176
        lxdream_set_global_config_value( key, buf );
nkeynes@1034
   177
    }
nkeynes@1034
   178
}
nkeynes@1034
   179
nkeynes@460
   180
void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
nkeynes@450
   181
{
nkeynes@475
   182
    if( param->value != value ) {
nkeynes@736
   183
        if( param->value != param->default_value && param->value != NULL ) {
nkeynes@736
   184
            free( param->value );
nkeynes@736
   185
        }
nkeynes@736
   186
        param->value = g_strdup(value);
nkeynes@450
   187
    }
nkeynes@450
   188
}
nkeynes@450
   189
nkeynes@460
   190
void lxdream_set_global_config_value( int key, const gchar *value )
nkeynes@460
   191
{
nkeynes@460
   192
    lxdream_set_config_value(&global_config[key], value);
nkeynes@460
   193
}
nkeynes@460
   194
nkeynes@759
   195
const struct lxdream_config_entry * lxdream_get_config_entry( int key )
nkeynes@724
   196
{
nkeynes@724
   197
    return &global_config[key];
nkeynes@724
   198
}
nkeynes@724
   199
nkeynes@458
   200
gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
nkeynes@458
   201
{
nkeynes@458
   202
    int i;
nkeynes@458
   203
    for( i=0; group->params[i].key != NULL; i++ ) {
nkeynes@736
   204
        if( strcasecmp( group->params[i].key, key ) == 0 ) {
nkeynes@736
   205
            lxdream_set_config_value( &group->params[i], value );
nkeynes@736
   206
            return TRUE;
nkeynes@736
   207
        }
nkeynes@458
   208
    }
nkeynes@458
   209
    return FALSE;
nkeynes@458
   210
}
nkeynes@458
   211
nkeynes@460
   212
void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
nkeynes@460
   213
{
nkeynes@460
   214
    int i;
nkeynes@460
   215
    for( i=0; src[i].key != NULL; i++ ) {
nkeynes@736
   216
        lxdream_set_config_value( &dest[i], src[i].value );
nkeynes@460
   217
    }
nkeynes@460
   218
}
nkeynes@460
   219
nkeynes@450
   220
gboolean lxdream_load_config( )
nkeynes@450
   221
{
nkeynes@450
   222
    if( lxdream_config_load_filename == NULL ) {
nkeynes@736
   223
        lxdream_find_config();
nkeynes@450
   224
    }
nkeynes@450
   225
    return lxdream_load_config_file(lxdream_config_load_filename);
nkeynes@450
   226
}
nkeynes@450
   227
nkeynes@450
   228
gboolean lxdream_save_config( )
nkeynes@450
   229
{
nkeynes@450
   230
    if( lxdream_config_save_filename == NULL ) {
nkeynes@736
   231
        lxdream_find_config();
nkeynes@450
   232
    }
nkeynes@450
   233
    return lxdream_save_config_file(lxdream_config_save_filename);
nkeynes@450
   234
}
nkeynes@450
   235
nkeynes@450
   236
gboolean lxdream_load_config_file( const gchar *filename )
nkeynes@450
   237
{
nkeynes@450
   238
    FILE *f;
nkeynes@450
   239
    gboolean result;
nkeynes@450
   240
nkeynes@450
   241
    if( access(filename, F_OK) != 0 ) {
nkeynes@736
   242
        INFO( "Configuration file '%s' does not exist, creating from defaults" );
nkeynes@736
   243
        lxdream_set_default_config();
nkeynes@736
   244
        lxdream_save_config();
nkeynes@450
   245
    }
nkeynes@450
   246
nkeynes@450
   247
    f = fopen(filename, "ro");
nkeynes@450
   248
    if( f == NULL ) {
nkeynes@736
   249
        ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
nkeynes@736
   250
        lxdream_set_default_config();
nkeynes@736
   251
        return FALSE;
nkeynes@450
   252
    }
nkeynes@450
   253
nkeynes@450
   254
    result = lxdream_load_config_stream( f );
nkeynes@450
   255
    fclose(f);
nkeynes@450
   256
    return result;
nkeynes@450
   257
}
nkeynes@450
   258
nkeynes@450
   259
gboolean lxdream_load_config_stream( FILE *f )
nkeynes@450
   260
{
nkeynes@450
   261
nkeynes@450
   262
    char buf[512];
nkeynes@450
   263
    int maple_device = -1, maple_subdevice = -1;
nkeynes@450
   264
    struct lxdream_config_group devgroup;
nkeynes@450
   265
    struct lxdream_config_group *group = NULL;
nkeynes@450
   266
    maple_device_t device = NULL;
nkeynes@450
   267
    lxdream_set_default_config();
nkeynes@450
   268
nkeynes@450
   269
    while( fgets( buf, sizeof(buf), f ) != NULL ) {
nkeynes@736
   270
        g_strstrip(buf);
nkeynes@736
   271
        if( buf[0] == '#' )
nkeynes@736
   272
            continue;
nkeynes@736
   273
        if( *buf == '[' ) {
nkeynes@736
   274
            char *p = strchr(buf, ']');
nkeynes@736
   275
            if( p != NULL ) {
nkeynes@736
   276
                struct lxdream_config_group *tmp_group;
nkeynes@736
   277
                maple_device = maple_subdevice = -1;
nkeynes@736
   278
                *p = '\0';
nkeynes@736
   279
                g_strstrip(buf+1);
nkeynes@736
   280
                tmp_group = &lxdream_config_root[0];
nkeynes@736
   281
                while( tmp_group->key != NULL ) {
nkeynes@736
   282
                    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
nkeynes@736
   283
                        group = tmp_group;
nkeynes@736
   284
                        break;
nkeynes@736
   285
                    }
nkeynes@736
   286
                    tmp_group++;
nkeynes@736
   287
                }
nkeynes@736
   288
            }
nkeynes@736
   289
        } else if( group != NULL ) {
nkeynes@736
   290
            char *value = strchr( buf, '=' );
nkeynes@736
   291
            if( value != NULL ) {
nkeynes@736
   292
                struct lxdream_config_entry *param = group->params;
nkeynes@736
   293
                *value = '\0';
nkeynes@736
   294
                value++;
nkeynes@736
   295
                g_strstrip(buf);
nkeynes@736
   296
                g_strstrip(value);
nkeynes@736
   297
                if( strcmp(group->key,"controllers") == 0  ) {
nkeynes@736
   298
                    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
nkeynes@736
   299
                        maple_device = strtoul( buf+7, NULL, 0 );
nkeynes@736
   300
                        if( maple_device < 0 || maple_device > 3 ) {
nkeynes@736
   301
                            ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
nkeynes@736
   302
                            continue;
nkeynes@736
   303
                        }
nkeynes@736
   304
                        maple_subdevice = 0;
nkeynes@736
   305
                        device = maple_new_device( value );
nkeynes@736
   306
                        if( device == NULL ) {
nkeynes@736
   307
                            ERROR( "Unrecognized device '%s'", value );
nkeynes@736
   308
                        } else {
nkeynes@736
   309
                            devgroup.key = "controllers";
nkeynes@736
   310
                            devgroup.params = maple_get_device_config(device);
nkeynes@736
   311
                            maple_attach_device( device, maple_device, maple_subdevice );
nkeynes@736
   312
                            group = &devgroup;
nkeynes@736
   313
                        }
nkeynes@736
   314
                        continue;
nkeynes@736
   315
                    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
nkeynes@736
   316
                        maple_subdevice = strtoul( buf+10, NULL, 0 );
nkeynes@736
   317
                        if( maple_device == -1 ) {
nkeynes@736
   318
                            ERROR( "Subdevice not allowed without primary device" );
nkeynes@736
   319
                        } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
nkeynes@736
   320
                            ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
nkeynes@736
   321
                        } else if( (device = maple_new_device(value)) == NULL ) {
nkeynes@736
   322
                            ERROR( "Unrecognized subdevice '%s'", value );
nkeynes@736
   323
                        } else {
nkeynes@736
   324
                            devgroup.key = "controllers";
nkeynes@736
   325
                            devgroup.params = maple_get_device_config(device);
nkeynes@736
   326
                            maple_attach_device( device, maple_device, maple_subdevice );
nkeynes@736
   327
                            group = &devgroup;
nkeynes@736
   328
                        }
nkeynes@736
   329
                        continue;
nkeynes@736
   330
                    }
nkeynes@736
   331
                }
nkeynes@736
   332
                while( param->key != NULL ) {
nkeynes@736
   333
                    if( strcasecmp( param->key, buf ) == 0 ) {
nkeynes@736
   334
                        param->value = g_strdup(value);
nkeynes@736
   335
                        break;
nkeynes@736
   336
                    }
nkeynes@736
   337
                    param++;
nkeynes@736
   338
                }
nkeynes@736
   339
            }
nkeynes@736
   340
        }
nkeynes@450
   341
    }
nkeynes@450
   342
    return TRUE;
nkeynes@450
   343
}
nkeynes@450
   344
nkeynes@450
   345
gboolean lxdream_save_config_file( const gchar *filename )
nkeynes@450
   346
{
nkeynes@450
   347
    FILE *f = fopen(filename, "wo");
nkeynes@450
   348
    gboolean result;
nkeynes@450
   349
    if( f == NULL ) {
nkeynes@736
   350
        ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
nkeynes@736
   351
        return FALSE;
nkeynes@450
   352
    }
nkeynes@450
   353
    result = lxdream_save_config_stream(f);
nkeynes@450
   354
    fclose(f);
nkeynes@450
   355
    return TRUE;
nkeynes@450
   356
}    
nkeynes@450
   357
nkeynes@450
   358
gboolean lxdream_save_config_stream( FILE *f )
nkeynes@450
   359
{
nkeynes@450
   360
    struct lxdream_config_group *group = &lxdream_config_root[0];
nkeynes@736
   361
nkeynes@450
   362
    while( group->key != NULL ) {
nkeynes@736
   363
        struct lxdream_config_entry *entry = group->params;
nkeynes@736
   364
        fprintf( f, "[%s]\n", group->key );
nkeynes@736
   365
nkeynes@736
   366
        if( entry != NULL ) {
nkeynes@736
   367
            while( entry->key != NULL ) {
nkeynes@736
   368
                if( entry->value != NULL ) {
nkeynes@736
   369
                    fprintf( f, "%s = %s\n", entry->key, entry->value );
nkeynes@736
   370
                }
nkeynes@736
   371
                entry++;
nkeynes@736
   372
            }
nkeynes@736
   373
        } else if( strcmp(group->key, "controllers") == 0 ) {
nkeynes@736
   374
            int i,j;
nkeynes@736
   375
            for( i=0; i<4; i++ ) {
nkeynes@736
   376
                for( j=0; j<6; j++ ) {
nkeynes@736
   377
                    maple_device_t dev = maple_get_device( i, j );
nkeynes@736
   378
                    if( dev != NULL ) {
nkeynes@736
   379
                        if( j == 0 )
nkeynes@736
   380
                            fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
nkeynes@736
   381
                        else 
nkeynes@736
   382
                            fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
nkeynes@736
   383
                        if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
nkeynes@736
   384
                            while( entry->key != NULL ) {
nkeynes@736
   385
                                if( entry->value != NULL ) {
nkeynes@736
   386
                                    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
nkeynes@736
   387
                                }
nkeynes@736
   388
                                entry++;
nkeynes@736
   389
                            }
nkeynes@736
   390
                        }
nkeynes@736
   391
                    }
nkeynes@736
   392
                }
nkeynes@736
   393
            }
nkeynes@736
   394
        }
nkeynes@736
   395
        fprintf( f, "\n" );
nkeynes@736
   396
        group++;
nkeynes@450
   397
    }
nkeynes@450
   398
    return TRUE;
nkeynes@450
   399
}
.