Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1024:c67f2d61ab97
prev1015:ad448bedc48a
next1034:7044e01148f0
author nkeynes
date Sat Jun 13 00:50:48 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Build drivers with library dependencies as shared objects (ie plugins)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * User configuration support
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <unistd.h>
    20 #include <stdio.h>
    21 #include <errno.h>
    22 #include <stdlib.h>
    23 #include <string.h>
    24 #include <glib/gmem.h>
    25 #include <glib/gstrfuncs.h>
    26 #include "dream.h"
    27 #include "config.h"
    28 #include "maple/maple.h"
    30 #define MAX_ROOT_GROUPS 16
    32 extern struct lxdream_config_entry alsa_config[];
    33 extern struct lxdream_config_entry hotkeys_config[];
    35 gboolean lxdream_load_config_file( const gchar *filename );
    36 gboolean lxdream_save_config_file( const gchar *filename );
    37 gboolean lxdream_load_config_stream( FILE *f );
    38 gboolean lxdream_save_config_stream( FILE *f );
    40 static struct lxdream_config_entry global_config[] =
    41        {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, "dcboot.rom" },
    42         { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, "dcflash.rom" },
    43         { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
    44         { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, "save" },
    45         { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
    46         { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
    47         { "recent", NULL, CONFIG_TYPE_FILE, NULL },
    48         { NULL, CONFIG_TYPE_NONE }};
    50 static struct lxdream_config_entry serial_config[] =
    51        {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
    52         { NULL, CONFIG_TYPE_NONE }};
    54 struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] = 
    55        {{ "global", global_config },
    56         { "controllers", NULL },
    57         { "hotkeys", hotkeys_config },
    58         { "serial", serial_config },
    59         { NULL, CONFIG_TYPE_NONE }};
    61 static gchar *lxdream_config_load_filename = NULL;
    62 static gchar *lxdream_config_save_filename = NULL;
    64 void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
    65 {
    66     int i;
    67     for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
    68         if( lxdream_config_root[i].key == NULL ) {
    69             lxdream_config_root[i].key = key;
    70             lxdream_config_root[i].params = group;
    71             lxdream_config_root[i+1].key = NULL;
    72             lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
    73             return;
    74         }
    75     }
    76     ERROR( "Unable to register config group '%s': Too many configuration groups", key );
    77 }
    79 gboolean lxdream_find_config()
    80 {
    81     gboolean result = TRUE;
    82     char *home = getenv("HOME");
    83     if( lxdream_config_save_filename == NULL ) {
    84         lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    85     }
    86     if( lxdream_config_load_filename == NULL ) {
    87         char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
    88         if( access(lxdream_config_save_filename, R_OK) == 0 ) {
    89             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    90             g_free(sysconfig);
    91         } else if( access( sysconfig, R_OK ) == 0 ) {
    92             lxdream_config_load_filename = sysconfig;
    93         } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    94             lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
    95             g_free(sysconfig);
    96         } else {
    97             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    98             g_free(sysconfig);
    99             result = FALSE;
   100         }	
   101     }
   102     return result;
   103 }
   105 void lxdream_set_config_filename( const gchar *filename )
   106 {
   107     if( lxdream_config_load_filename != NULL ) {
   108         g_free(lxdream_config_load_filename);
   109     }
   110     lxdream_config_load_filename = g_strdup(filename);
   111     if( lxdream_config_save_filename != NULL ) {
   112         g_free(lxdream_config_save_filename);
   113     }
   114     lxdream_config_save_filename = g_strdup(filename);
   115 }
   117 void lxdream_set_default_config( )
   118 {
   119     struct lxdream_config_group *group = lxdream_config_root;
   120     while( group->key != NULL ) {
   121         struct lxdream_config_entry *param = group->params;
   122         if( param != NULL ) {
   123             while( param->key != NULL ) {
   124                 if( param->value != param->default_value ) {
   125                     if( param->value != NULL )
   126                         free( param->value );
   127                     param->value = (gchar *)param->default_value;
   128                 }
   129                 param++;
   130             }
   131         }
   132         group++;
   133     }
   134     maple_detach_all();
   135 }
   137 const gchar *lxdream_get_config_value( int key )
   138 {
   139     return global_config[key].value;
   140 }
   142 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
   143 {
   144     if( param->value != value ) {
   145         if( param->value != param->default_value && param->value != NULL ) {
   146             free( param->value );
   147         }
   148         param->value = g_strdup(value);
   149     }
   150 }
   152 void lxdream_set_global_config_value( int key, const gchar *value )
   153 {
   154     lxdream_set_config_value(&global_config[key], value);
   155 }
   157 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
   158 {
   159     return &global_config[key];
   160 }
   162 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
   163 {
   164     int i;
   165     for( i=0; group->params[i].key != NULL; i++ ) {
   166         if( strcasecmp( group->params[i].key, key ) == 0 ) {
   167             lxdream_set_config_value( &group->params[i], value );
   168             return TRUE;
   169         }
   170     }
   171     return FALSE;
   172 }
   174 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
   175 {
   176     int i;
   177     for( i=0; src[i].key != NULL; i++ ) {
   178         lxdream_set_config_value( &dest[i], src[i].value );
   179     }
   180 }
   182 gboolean lxdream_load_config( )
   183 {
   184     if( lxdream_config_load_filename == NULL ) {
   185         lxdream_find_config();
   186     }
   187     return lxdream_load_config_file(lxdream_config_load_filename);
   188 }
   190 gboolean lxdream_save_config( )
   191 {
   192     if( lxdream_config_save_filename == NULL ) {
   193         lxdream_find_config();
   194     }
   195     return lxdream_save_config_file(lxdream_config_save_filename);
   196 }
   198 gboolean lxdream_load_config_file( const gchar *filename )
   199 {
   200     FILE *f;
   201     gboolean result;
   203     if( access(filename, F_OK) != 0 ) {
   204         INFO( "Configuration file '%s' does not exist, creating from defaults" );
   205         lxdream_set_default_config();
   206         lxdream_save_config();
   207     }
   209     f = fopen(filename, "ro");
   210     if( f == NULL ) {
   211         ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   212         lxdream_set_default_config();
   213         return FALSE;
   214     }
   216     result = lxdream_load_config_stream( f );
   217     fclose(f);
   218     return result;
   219 }
   221 gboolean lxdream_load_config_stream( FILE *f )
   222 {
   224     char buf[512];
   225     int maple_device = -1, maple_subdevice = -1;
   226     struct lxdream_config_group devgroup;
   227     struct lxdream_config_group *group = NULL;
   228     maple_device_t device = NULL;
   229     lxdream_set_default_config();
   231     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   232         g_strstrip(buf);
   233         if( buf[0] == '#' )
   234             continue;
   235         if( *buf == '[' ) {
   236             char *p = strchr(buf, ']');
   237             if( p != NULL ) {
   238                 struct lxdream_config_group *tmp_group;
   239                 maple_device = maple_subdevice = -1;
   240                 *p = '\0';
   241                 g_strstrip(buf+1);
   242                 tmp_group = &lxdream_config_root[0];
   243                 while( tmp_group->key != NULL ) {
   244                     if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   245                         group = tmp_group;
   246                         break;
   247                     }
   248                     tmp_group++;
   249                 }
   250             }
   251         } else if( group != NULL ) {
   252             char *value = strchr( buf, '=' );
   253             if( value != NULL ) {
   254                 struct lxdream_config_entry *param = group->params;
   255                 *value = '\0';
   256                 value++;
   257                 g_strstrip(buf);
   258                 g_strstrip(value);
   259                 if( strcmp(group->key,"controllers") == 0  ) {
   260                     if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   261                         maple_device = strtoul( buf+7, NULL, 0 );
   262                         if( maple_device < 0 || maple_device > 3 ) {
   263                             ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   264                             continue;
   265                         }
   266                         maple_subdevice = 0;
   267                         device = maple_new_device( value );
   268                         if( device == NULL ) {
   269                             ERROR( "Unrecognized device '%s'", value );
   270                         } else {
   271                             devgroup.key = "controllers";
   272                             devgroup.params = maple_get_device_config(device);
   273                             maple_attach_device( device, maple_device, maple_subdevice );
   274                             group = &devgroup;
   275                         }
   276                         continue;
   277                     } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   278                         maple_subdevice = strtoul( buf+10, NULL, 0 );
   279                         if( maple_device == -1 ) {
   280                             ERROR( "Subdevice not allowed without primary device" );
   281                         } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   282                             ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   283                         } else if( (device = maple_new_device(value)) == NULL ) {
   284                             ERROR( "Unrecognized subdevice '%s'", value );
   285                         } else {
   286                             devgroup.key = "controllers";
   287                             devgroup.params = maple_get_device_config(device);
   288                             maple_attach_device( device, maple_device, maple_subdevice );
   289                             group = &devgroup;
   290                         }
   291                         continue;
   292                     }
   293                 }
   294                 while( param->key != NULL ) {
   295                     if( strcasecmp( param->key, buf ) == 0 ) {
   296                         param->value = g_strdup(value);
   297                         break;
   298                     }
   299                     param++;
   300                 }
   301             }
   302         }
   303     }
   304     return TRUE;
   305 }
   307 gboolean lxdream_save_config_file( const gchar *filename )
   308 {
   309     FILE *f = fopen(filename, "wo");
   310     gboolean result;
   311     if( f == NULL ) {
   312         ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   313         return FALSE;
   314     }
   315     result = lxdream_save_config_stream(f);
   316     fclose(f);
   317     return TRUE;
   318 }    
   320 gboolean lxdream_save_config_stream( FILE *f )
   321 {
   322     struct lxdream_config_group *group = &lxdream_config_root[0];
   324     while( group->key != NULL ) {
   325         struct lxdream_config_entry *entry = group->params;
   326         fprintf( f, "[%s]\n", group->key );
   328         if( entry != NULL ) {
   329             while( entry->key != NULL ) {
   330                 if( entry->value != NULL ) {
   331                     fprintf( f, "%s = %s\n", entry->key, entry->value );
   332                 }
   333                 entry++;
   334             }
   335         } else if( strcmp(group->key, "controllers") == 0 ) {
   336             int i,j;
   337             for( i=0; i<4; i++ ) {
   338                 for( j=0; j<6; j++ ) {
   339                     maple_device_t dev = maple_get_device( i, j );
   340                     if( dev != NULL ) {
   341                         if( j == 0 )
   342                             fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   343                         else 
   344                             fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   345                         if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
   346                             while( entry->key != NULL ) {
   347                                 if( entry->value != NULL ) {
   348                                     fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   349                                 }
   350                                 entry++;
   351                             }
   352                         }
   353                     }
   354                 }
   355             }
   356         }
   357         fprintf( f, "\n" );
   358         group++;
   359     }
   360     return TRUE;
   361 }
.