Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1038:f220d18c0615
prev1036:af7b0c5905dd
next1041:5fcc39857c5c
author nkeynes
date Thu Jun 25 21:21:18 2009 +0000 (14 years ago)
permissions -rw-r--r--
last change Add quick state bits to the menus
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 <ctype.h>
    22 #include <errno.h>
    23 #include <stdlib.h>
    24 #include <string.h>
    25 #include <wordexp.h>
    26 #include <glib/gmem.h>
    27 #include <glib/gstrfuncs.h>
    28 #include <sys/types.h>
    29 #include <sys/stat.h>
    30 #include "dream.h"
    31 #include "config.h"
    32 #include "maple/maple.h"
    34 #define MAX_ROOT_GROUPS 16
    36 extern struct lxdream_config_entry alsa_config[];
    37 extern struct lxdream_config_entry hotkeys_config[];
    39 gboolean lxdream_load_config_file( const gchar *filename );
    40 gboolean lxdream_save_config_file( const gchar *filename );
    41 gboolean lxdream_load_config_stream( FILE *f );
    42 gboolean lxdream_save_config_stream( FILE *f );
    44 static struct lxdream_config_entry global_config[] =
    45        {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, NULL },
    46         { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, NULL },
    47         { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
    48         { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, NULL },
    49         { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, NULL },
    50         { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, NULL },
    51         { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
    52         { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
    53         { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
    54         { "quick state", NULL, CONFIG_TYPE_INTEGER, "0" },
    55         { NULL, CONFIG_TYPE_NONE }};
    57 static struct lxdream_config_entry serial_config[] =
    58        {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
    59         { NULL, CONFIG_TYPE_NONE }};
    61 struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] = 
    62        {{ "global", global_config },
    63         { "controllers", NULL },
    64         { "hotkeys", hotkeys_config },
    65         { "serial", serial_config },
    66         { NULL, CONFIG_TYPE_NONE }};
    68 static gchar *lxdream_config_load_filename = NULL;
    69 static gchar *lxdream_config_save_filename = NULL;
    71 void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
    72 {
    73     int i;
    74     for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
    75         if( lxdream_config_root[i].key == NULL ) {
    76             lxdream_config_root[i].key = key;
    77             lxdream_config_root[i].params = group;
    78             lxdream_config_root[i+1].key = NULL;
    79             lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
    80             return;
    81         }
    82     }
    83     ERROR( "Unable to register config group '%s': Too many configuration groups", key );
    84 }
    86 gboolean lxdream_find_config()
    87 {
    88     gboolean result = TRUE;
    89     char *home = getenv("HOME");
    90     if( lxdream_config_save_filename == NULL ) {
    91         /* For compatibility, look for the old ~/.lxdreamrc first. Use it if 
    92          * found, otherwise use the new ~/.lxdream/lxdreamrc.
    93          */
    94         lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    95         if( access(lxdream_config_save_filename, R_OK) != 0 ) {
    96             g_free(lxdream_config_save_filename);
    97             const char *user_path = get_user_data_path();
    98             lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
    99         }
   100     }
   101     if( lxdream_config_load_filename == NULL ) {
   102         char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
   103         if( access(lxdream_config_save_filename, R_OK) == 0 ) {
   104             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
   105             g_free(sysconfig);
   106         } else if( access( sysconfig, R_OK ) == 0 ) {
   107             lxdream_config_load_filename = sysconfig;
   108         } else {
   109             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
   110             g_free(sysconfig);
   111             result = FALSE;
   112         }	
   113     }
   114     return result;
   115 }
   117 void lxdream_set_config_filename( const gchar *filename )
   118 {
   119     if( lxdream_config_load_filename != NULL ) {
   120         g_free(lxdream_config_load_filename);
   121     }
   122     lxdream_config_load_filename = g_strdup(filename);
   123     if( lxdream_config_save_filename != NULL ) {
   124         g_free(lxdream_config_save_filename);
   125     }
   126     lxdream_config_save_filename = g_strdup(filename);
   127 }
   129 void lxdream_set_default_config( )
   130 {
   131     /* Construct platform dependent defaults */
   132     const gchar *user_path = get_user_data_path();
   133     global_config[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path ); 
   134     global_config[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path ); 
   135     global_config[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path ); 
   136     global_config[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path ); 
   137     global_config[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path ); 
   139     /* Copy defaults into main values */
   140     struct lxdream_config_group *group = lxdream_config_root;
   141     while( group->key != NULL ) {
   142         struct lxdream_config_entry *param = group->params;
   143         if( param != NULL ) {
   144             while( param->key != NULL ) {
   145                 if( param->value != param->default_value ) {
   146                     if( param->value != NULL )
   147                         free( param->value );
   148                     param->value = (gchar *)param->default_value;
   149                 }
   150                 param++;
   151             }
   152         }
   153         group++;
   154     }
   155     maple_detach_all();
   156 }
   158 const gchar *lxdream_get_global_config_value( int key )
   159 {
   160     return global_config[key].value;
   161 }
   163 GList *lxdream_get_global_config_list_value( int key )
   164 {
   165     GList *result = NULL;
   166     const gchar *str = lxdream_get_global_config_value( key );
   167     if( str != NULL ) {
   168         gchar **strv = g_strsplit(str, ":",0);
   169         int i;
   170         for( i=0; strv[i] != NULL; i++ ) {
   171             result = g_list_append( result, g_strdup(strv[i]) );
   172         }
   173         g_strfreev(strv);
   174     }
   175     return result;
   176 }
   178 void lxdream_set_global_config_list_value( int key, const GList *list )
   179 {
   180     if( list == NULL ) {
   181         lxdream_set_global_config_value( key, NULL );
   182     } else {
   183         const GList *ptr;
   184         int size = 0;
   186         for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
   187             size += strlen( (gchar *)ptr->data ) + 1;
   188         }
   189         char buf[size];
   190         strcpy( buf, (gchar *)list->data );
   191         for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
   192             strcat( buf, ":" );
   193             strcat( buf, (gchar *)ptr->data );
   194         }
   195         lxdream_set_global_config_value( key, buf );
   196     }
   197 }
   199 gchar *get_expanded_path( const gchar *input )
   200 {
   201     wordexp_t we;
   202     if( input == NULL ) {
   203         return NULL;
   204     }
   205     memset(&we,0,sizeof(we));
   206     int result = wordexp(input, &we, WRDE_NOCMD);
   207     if( result != 0 || we.we_wordc == 0 ) {
   208         /* On failure, return the original input unchanged */
   209         return g_strdup(input);
   210     } else {
   211         /* On success, concatenate all 'words' together into a single 
   212          * space-separated string
   213          */
   214         int length = we.we_wordc, i;
   215         gchar *result, *p;
   217         for( i=0; i<we.we_wordc; i++ ) {
   218             length += strlen(we.we_wordv[i]);
   219         }
   220         p = result = g_malloc(length);
   221         for( i=0; i<we.we_wordc; i++ ) {
   222             if( i != 0 )
   223                 *p++ = ' ';
   224             strcpy( p, we.we_wordv[i] );
   225             p += strlen(p);
   226         }
   227         wordfree(&we);
   228         return result;
   229     }        
   230 }
   232 /**
   233  * Test if we need to escape a path to prevent substitution mangling. 
   234  * @return TRUE if the input value contains any character that doesn't
   235  * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
   236  * but is safe)
   237  */
   238 gboolean path_needs_escaping( const gchar *value )
   239 {
   240    const gchar *p = value;
   241    while( *p ) {
   242        if( !isalnum(*p) && *p != '.' && *p != '_' &&
   243                *p != '@' && *p != '%' && *p != '/' ) {
   244            return TRUE;
   245        }
   246        p++;
   247    }
   248    return FALSE;
   249 }
   251 gchar *get_escaped_path( const gchar *value )
   252 {
   253     if( value != NULL && path_needs_escaping(value) ) {
   254         /* Escape with "", and backslash the remaining characters:
   255          *   \ " $ `
   256          */
   257         char buf[strlen(value)*2+3];  
   258         const char *s = value;
   259         char *p = buf;
   260         *p++ = '\"';
   261         while( *s ) {
   262             if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
   263                 *p++ = '\\';
   264             }
   265             *p++ = *s++;
   266         }
   267         *p++ = '\"';
   268         *p = '\0';
   269         return g_strdup(buf);
   270     } else {
   271         return g_strdup(value);
   272     }
   273 }
   275 gchar *lxdream_get_global_config_path_value( int key )
   276 {
   277     const gchar *str = lxdream_get_global_config_value(key);
   278     if( str == NULL ) {
   279         return NULL;
   280     } else {
   281         return get_expanded_path(str);
   282     }
   283 }
   285 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
   286 {
   287     gchar *temp = get_escaped_path(value);
   288     lxdream_set_global_config_value(key,temp);
   289     g_free(temp);
   290     return lxdream_get_global_config_value(key);
   291 }
   293 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
   294 {
   295     if( param->value != value ) {
   296         if( param->value != param->default_value && param->value != NULL ) {
   297             free( param->value );
   298         }
   299         param->value = g_strdup(value);
   300     }
   301 }
   303 void lxdream_set_global_config_value( int key, const gchar *value )
   304 {
   305     lxdream_set_config_value(&global_config[key], value);
   306 }
   308 const struct lxdream_config_entry * lxdream_get_global_config_entry( int key )
   309 {
   310     return &global_config[key];
   311 }
   313 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
   314 {
   315     int i;
   316     for( i=0; group->params[i].key != NULL; i++ ) {
   317         if( strcasecmp( group->params[i].key, key ) == 0 ) {
   318             lxdream_set_config_value( &group->params[i], value );
   319             return TRUE;
   320         }
   321     }
   322     return FALSE;
   323 }
   325 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
   326 {
   327     int i;
   328     for( i=0; src[i].key != NULL; i++ ) {
   329         lxdream_set_config_value( &dest[i], src[i].value );
   330     }
   331 }
   333 gboolean lxdream_load_config( )
   334 {
   335     if( lxdream_config_load_filename == NULL ) {
   336         lxdream_find_config();
   337     }
   338     return lxdream_load_config_file(lxdream_config_load_filename);
   339 }
   341 gboolean lxdream_save_config( )
   342 {
   343     if( lxdream_config_save_filename == NULL ) {
   344         lxdream_find_config();
   345     }
   346     return lxdream_save_config_file(lxdream_config_save_filename);
   347 }
   349 gboolean lxdream_load_config_file( const gchar *filename )
   350 {
   351     FILE *f;
   352     gboolean result;
   354     if( access(filename, F_OK) != 0 ) {
   355         INFO( "Configuration file '%s' does not exist, creating from defaults" );
   356         lxdream_set_default_config();
   357         lxdream_save_config();
   358     }
   360     f = fopen(filename, "ro");
   361     if( f == NULL ) {
   362         ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   363         lxdream_set_default_config();
   364         return FALSE;
   365     }
   367     result = lxdream_load_config_stream( f );
   368     fclose(f);
   369     return result;
   370 }
   372 gboolean lxdream_load_config_stream( FILE *f )
   373 {
   375     char buf[512];
   376     int maple_device = -1, maple_subdevice = -1;
   377     struct lxdream_config_group devgroup;
   378     struct lxdream_config_group *group = NULL;
   379     maple_device_t device = NULL;
   380     lxdream_set_default_config();
   382     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   383         g_strstrip(buf);
   384         if( buf[0] == '#' )
   385             continue;
   386         if( *buf == '[' ) {
   387             char *p = strchr(buf, ']');
   388             if( p != NULL ) {
   389                 struct lxdream_config_group *tmp_group;
   390                 maple_device = maple_subdevice = -1;
   391                 *p = '\0';
   392                 g_strstrip(buf+1);
   393                 tmp_group = &lxdream_config_root[0];
   394                 while( tmp_group->key != NULL ) {
   395                     if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   396                         group = tmp_group;
   397                         break;
   398                     }
   399                     tmp_group++;
   400                 }
   401             }
   402         } else if( group != NULL ) {
   403             char *value = strchr( buf, '=' );
   404             if( value != NULL ) {
   405                 struct lxdream_config_entry *param = group->params;
   406                 *value = '\0';
   407                 value++;
   408                 g_strstrip(buf);
   409                 g_strstrip(value);
   410                 if( strcmp(group->key,"controllers") == 0  ) {
   411                     if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   412                         maple_device = strtoul( buf+7, NULL, 0 );
   413                         if( maple_device < 0 || maple_device > 3 ) {
   414                             ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   415                             continue;
   416                         }
   417                         maple_subdevice = 0;
   418                         device = maple_new_device( value );
   419                         if( device == NULL ) {
   420                             ERROR( "Unrecognized device '%s'", value );
   421                         } else {
   422                             devgroup.key = "controllers";
   423                             devgroup.params = maple_get_device_config(device);
   424                             maple_attach_device( device, maple_device, maple_subdevice );
   425                             group = &devgroup;
   426                         }
   427                         continue;
   428                     } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   429                         maple_subdevice = strtoul( buf+10, NULL, 0 );
   430                         if( maple_device == -1 ) {
   431                             ERROR( "Subdevice not allowed without primary device" );
   432                         } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   433                             ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   434                         } else if( (device = maple_new_device(value)) == NULL ) {
   435                             ERROR( "Unrecognized subdevice '%s'", value );
   436                         } else {
   437                             devgroup.key = "controllers";
   438                             devgroup.params = maple_get_device_config(device);
   439                             maple_attach_device( device, maple_device, maple_subdevice );
   440                             group = &devgroup;
   441                         }
   442                         continue;
   443                     }
   444                 }
   445                 while( param->key != NULL ) {
   446                     if( strcasecmp( param->key, buf ) == 0 ) {
   447                         param->value = g_strdup(value);
   448                         break;
   449                     }
   450                     param++;
   451                 }
   452             }
   453         }
   454     }
   455     return TRUE;
   456 }
   458 gboolean lxdream_save_config_file( const gchar *filename )
   459 {
   460     FILE *f = fopen(filename, "wo");
   461     gboolean result;
   462     if( f == NULL ) {
   463         ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   464         return FALSE;
   465     }
   466     result = lxdream_save_config_stream(f);
   467     fclose(f);
   468     return TRUE;
   469 }    
   471 gboolean lxdream_save_config_stream( FILE *f )
   472 {
   473     struct lxdream_config_group *group = &lxdream_config_root[0];
   475     while( group->key != NULL ) {
   476         struct lxdream_config_entry *entry = group->params;
   477         fprintf( f, "[%s]\n", group->key );
   479         if( entry != NULL ) {
   480             while( entry->key != NULL ) {
   481                 if( entry->value != NULL ) {
   482                     fprintf( f, "%s = %s\n", entry->key, entry->value );
   483                 }
   484                 entry++;
   485             }
   486         } else if( strcmp(group->key, "controllers") == 0 ) {
   487             int i,j;
   488             for( i=0; i<4; i++ ) {
   489                 for( j=0; j<6; j++ ) {
   490                     maple_device_t dev = maple_get_device( i, j );
   491                     if( dev != NULL ) {
   492                         if( j == 0 )
   493                             fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   494                         else 
   495                             fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   496                         if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
   497                             while( entry->key != NULL ) {
   498                                 if( entry->value != NULL ) {
   499                                     fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   500                                 }
   501                                 entry++;
   502                             }
   503                         }
   504                     }
   505                 }
   506             }
   507         }
   508         fprintf( f, "\n" );
   509         group++;
   510     }
   511     return TRUE;
   512 }
   514 void lxdream_make_config_dir( )
   515 {
   516     const char *user_path = get_user_data_path();
   517     struct stat st;
   519     if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
   520             (st.st_mode & S_IFDIR) != 0 ) {
   521         /* All good */
   522         return;
   523     }
   525     if( mkdir( user_path, 0777 ) != 0 ) {
   526         ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
   527         return;
   528     }
   530     char *vmupath = g_strdup_printf( "%s/vmu", user_path );
   531     mkdir( vmupath, 0777 );
   532     g_free( vmupath );
   534     char *savepath = g_strdup_printf( "%s/save", user_path );
   535     mkdir( savepath, 0777 );
   536     g_free( vmupath );
   537 }
.