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