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