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
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         { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, "vmu" },
    46         { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
    47         { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
    48         { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
    49         { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
    50         { NULL, CONFIG_TYPE_NONE }};
    52 static struct lxdream_config_entry serial_config[] =
    53        {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
    54         { NULL, CONFIG_TYPE_NONE }};
    56 struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] = 
    57        {{ "global", global_config },
    58         { "controllers", NULL },
    59         { "hotkeys", hotkeys_config },
    60         { "serial", serial_config },
    61         { NULL, CONFIG_TYPE_NONE }};
    63 static gchar *lxdream_config_load_filename = NULL;
    64 static gchar *lxdream_config_save_filename = NULL;
    66 void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
    67 {
    68     int i;
    69     for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
    70         if( lxdream_config_root[i].key == NULL ) {
    71             lxdream_config_root[i].key = key;
    72             lxdream_config_root[i].params = group;
    73             lxdream_config_root[i+1].key = NULL;
    74             lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
    75             return;
    76         }
    77     }
    78     ERROR( "Unable to register config group '%s': Too many configuration groups", key );
    79 }
    81 gboolean lxdream_find_config()
    82 {
    83     gboolean result = TRUE;
    84     char *home = getenv("HOME");
    85     if( lxdream_config_save_filename == NULL ) {
    86         lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    87     }
    88     if( lxdream_config_load_filename == NULL ) {
    89         char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
    90         if( access(lxdream_config_save_filename, R_OK) == 0 ) {
    91             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    92             g_free(sysconfig);
    93         } else if( access( sysconfig, R_OK ) == 0 ) {
    94             lxdream_config_load_filename = sysconfig;
    95         } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    96             lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
    97             g_free(sysconfig);
    98         } else {
    99             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
   100             g_free(sysconfig);
   101             result = FALSE;
   102         }	
   103     }
   104     return result;
   105 }
   107 void lxdream_set_config_filename( const gchar *filename )
   108 {
   109     if( lxdream_config_load_filename != NULL ) {
   110         g_free(lxdream_config_load_filename);
   111     }
   112     lxdream_config_load_filename = g_strdup(filename);
   113     if( lxdream_config_save_filename != NULL ) {
   114         g_free(lxdream_config_save_filename);
   115     }
   116     lxdream_config_save_filename = g_strdup(filename);
   117 }
   119 void lxdream_set_default_config( )
   120 {
   121     struct lxdream_config_group *group = lxdream_config_root;
   122     while( group->key != NULL ) {
   123         struct lxdream_config_entry *param = group->params;
   124         if( param != NULL ) {
   125             while( param->key != NULL ) {
   126                 if( param->value != param->default_value ) {
   127                     if( param->value != NULL )
   128                         free( param->value );
   129                     param->value = (gchar *)param->default_value;
   130                 }
   131                 param++;
   132             }
   133         }
   134         group++;
   135     }
   136     maple_detach_all();
   137 }
   139 const gchar *lxdream_get_config_value( int key )
   140 {
   141     return global_config[key].value;
   142 }
   144 GList *lxdream_get_global_config_list_value( int key )
   145 {
   146     GList *result = NULL;
   147     const gchar *str = lxdream_get_config_value( key );
   148     if( str != NULL ) {
   149         gchar **strv = g_strsplit(str, ":",0);
   150         int i;
   151         for( i=0; strv[i] != NULL; i++ ) {
   152             result = g_list_append( result, g_strdup(strv[i]) );
   153         }
   154         g_strfreev(strv);
   155     }
   156     return result;
   157 }
   159 void lxdream_set_global_config_list_value( int key, const GList *list )
   160 {
   161     if( list == NULL ) {
   162         lxdream_set_global_config_value( key, NULL );
   163     } else {
   164         GList *ptr;
   165         int size = 0;
   167         for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
   168             size += strlen( (gchar *)ptr->data ) + 1;
   169         }
   170         char buf[size];
   171         strcpy( buf, (gchar *)list->data );
   172         for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
   173             strcat( buf, ":" );
   174             strcat( buf, (gchar *)ptr->data );
   175         }
   176         lxdream_set_global_config_value( key, buf );
   177     }
   178 }
   180 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
   181 {
   182     if( param->value != value ) {
   183         if( param->value != param->default_value && param->value != NULL ) {
   184             free( param->value );
   185         }
   186         param->value = g_strdup(value);
   187     }
   188 }
   190 void lxdream_set_global_config_value( int key, const gchar *value )
   191 {
   192     lxdream_set_config_value(&global_config[key], value);
   193 }
   195 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
   196 {
   197     return &global_config[key];
   198 }
   200 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
   201 {
   202     int i;
   203     for( i=0; group->params[i].key != NULL; i++ ) {
   204         if( strcasecmp( group->params[i].key, key ) == 0 ) {
   205             lxdream_set_config_value( &group->params[i], value );
   206             return TRUE;
   207         }
   208     }
   209     return FALSE;
   210 }
   212 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
   213 {
   214     int i;
   215     for( i=0; src[i].key != NULL; i++ ) {
   216         lxdream_set_config_value( &dest[i], src[i].value );
   217     }
   218 }
   220 gboolean lxdream_load_config( )
   221 {
   222     if( lxdream_config_load_filename == NULL ) {
   223         lxdream_find_config();
   224     }
   225     return lxdream_load_config_file(lxdream_config_load_filename);
   226 }
   228 gboolean lxdream_save_config( )
   229 {
   230     if( lxdream_config_save_filename == NULL ) {
   231         lxdream_find_config();
   232     }
   233     return lxdream_save_config_file(lxdream_config_save_filename);
   234 }
   236 gboolean lxdream_load_config_file( const gchar *filename )
   237 {
   238     FILE *f;
   239     gboolean result;
   241     if( access(filename, F_OK) != 0 ) {
   242         INFO( "Configuration file '%s' does not exist, creating from defaults" );
   243         lxdream_set_default_config();
   244         lxdream_save_config();
   245     }
   247     f = fopen(filename, "ro");
   248     if( f == NULL ) {
   249         ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   250         lxdream_set_default_config();
   251         return FALSE;
   252     }
   254     result = lxdream_load_config_stream( f );
   255     fclose(f);
   256     return result;
   257 }
   259 gboolean lxdream_load_config_stream( FILE *f )
   260 {
   262     char buf[512];
   263     int maple_device = -1, maple_subdevice = -1;
   264     struct lxdream_config_group devgroup;
   265     struct lxdream_config_group *group = NULL;
   266     maple_device_t device = NULL;
   267     lxdream_set_default_config();
   269     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   270         g_strstrip(buf);
   271         if( buf[0] == '#' )
   272             continue;
   273         if( *buf == '[' ) {
   274             char *p = strchr(buf, ']');
   275             if( p != NULL ) {
   276                 struct lxdream_config_group *tmp_group;
   277                 maple_device = maple_subdevice = -1;
   278                 *p = '\0';
   279                 g_strstrip(buf+1);
   280                 tmp_group = &lxdream_config_root[0];
   281                 while( tmp_group->key != NULL ) {
   282                     if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   283                         group = tmp_group;
   284                         break;
   285                     }
   286                     tmp_group++;
   287                 }
   288             }
   289         } else if( group != NULL ) {
   290             char *value = strchr( buf, '=' );
   291             if( value != NULL ) {
   292                 struct lxdream_config_entry *param = group->params;
   293                 *value = '\0';
   294                 value++;
   295                 g_strstrip(buf);
   296                 g_strstrip(value);
   297                 if( strcmp(group->key,"controllers") == 0  ) {
   298                     if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   299                         maple_device = strtoul( buf+7, NULL, 0 );
   300                         if( maple_device < 0 || maple_device > 3 ) {
   301                             ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   302                             continue;
   303                         }
   304                         maple_subdevice = 0;
   305                         device = maple_new_device( value );
   306                         if( device == NULL ) {
   307                             ERROR( "Unrecognized device '%s'", value );
   308                         } else {
   309                             devgroup.key = "controllers";
   310                             devgroup.params = maple_get_device_config(device);
   311                             maple_attach_device( device, maple_device, maple_subdevice );
   312                             group = &devgroup;
   313                         }
   314                         continue;
   315                     } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   316                         maple_subdevice = strtoul( buf+10, NULL, 0 );
   317                         if( maple_device == -1 ) {
   318                             ERROR( "Subdevice not allowed without primary device" );
   319                         } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   320                             ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   321                         } else if( (device = maple_new_device(value)) == NULL ) {
   322                             ERROR( "Unrecognized subdevice '%s'", value );
   323                         } else {
   324                             devgroup.key = "controllers";
   325                             devgroup.params = maple_get_device_config(device);
   326                             maple_attach_device( device, maple_device, maple_subdevice );
   327                             group = &devgroup;
   328                         }
   329                         continue;
   330                     }
   331                 }
   332                 while( param->key != NULL ) {
   333                     if( strcasecmp( param->key, buf ) == 0 ) {
   334                         param->value = g_strdup(value);
   335                         break;
   336                     }
   337                     param++;
   338                 }
   339             }
   340         }
   341     }
   342     return TRUE;
   343 }
   345 gboolean lxdream_save_config_file( const gchar *filename )
   346 {
   347     FILE *f = fopen(filename, "wo");
   348     gboolean result;
   349     if( f == NULL ) {
   350         ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   351         return FALSE;
   352     }
   353     result = lxdream_save_config_stream(f);
   354     fclose(f);
   355     return TRUE;
   356 }    
   358 gboolean lxdream_save_config_stream( FILE *f )
   359 {
   360     struct lxdream_config_group *group = &lxdream_config_root[0];
   362     while( group->key != NULL ) {
   363         struct lxdream_config_entry *entry = group->params;
   364         fprintf( f, "[%s]\n", group->key );
   366         if( entry != NULL ) {
   367             while( entry->key != NULL ) {
   368                 if( entry->value != NULL ) {
   369                     fprintf( f, "%s = %s\n", entry->key, entry->value );
   370                 }
   371                 entry++;
   372             }
   373         } else if( strcmp(group->key, "controllers") == 0 ) {
   374             int i,j;
   375             for( i=0; i<4; i++ ) {
   376                 for( j=0; j<6; j++ ) {
   377                     maple_device_t dev = maple_get_device( i, j );
   378                     if( dev != NULL ) {
   379                         if( j == 0 )
   380                             fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   381                         else 
   382                             fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   383                         if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
   384                             while( entry->key != NULL ) {
   385                                 if( entry->value != NULL ) {
   386                                     fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   387                                 }
   388                                 entry++;
   389                             }
   390                         }
   391                     }
   392                 }
   393             }
   394         }
   395         fprintf( f, "\n" );
   396         group++;
   397     }
   398     return TRUE;
   399 }
.