Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1144:00dd49743974
prev1077:136fc24d17ef
next1296:30ecee61f811
author nkeynes
date Fri Mar 02 23:49:10 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Android WIP:
* Rename gui_jni.c to gui_android.c - now quite android specific.
* Implement generic EGL driver with very minimal Java wrapper
* Run emulation in separate thread, and implement simple queue for
inter-thread communication.
* Add menu/action-bar items for start + reset
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 <sys/types.h>
    27 #include <sys/stat.h>
    28 #include "dreamcast.h"
    29 #include "config.h"
    30 #include "lxpaths.h"
    31 #include "maple/maple.h"
    33 #define MAX_ROOT_GROUPS 16
    35 extern struct lxdream_config_group hotkeys_group;
    36 extern struct lxdream_config_group serial_group;
    38 gboolean lxdream_load_config_file( const gchar *filename );
    39 gboolean lxdream_save_config_file( const gchar *filename );
    40 gboolean lxdream_load_config_stream( FILE *f );
    41 gboolean lxdream_save_config_stream( FILE *f );
    43 static struct lxdream_config_group global_group =
    44     { "global", dreamcast_config_changed, NULL, NULL,
    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 /**
    58  * Dummy group for controllers (handled specially)
    59  */
    60 static struct lxdream_config_group controllers_group =
    61     { "controllers", NULL, NULL, NULL, {{NULL, CONFIG_TYPE_NONE}} };
    63 struct lxdream_config_group *lxdream_config_root[MAX_ROOT_GROUPS+1] = 
    64        { &global_group, &controllers_group, &hotkeys_group, &serial_group, NULL };
    66 static gchar *lxdream_config_load_filename = NULL;
    67 static gchar *lxdream_config_save_filename = NULL;
    69 void lxdream_register_config_group( const gchar *key, lxdream_config_group_t group )
    70 {
    71     int i;
    72     for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
    73         if( lxdream_config_root[i] == NULL ) {
    74             lxdream_config_root[i] = group;
    75             lxdream_config_root[i+1] = NULL;
    76             return;
    77         }
    78     }
    79     ERROR( "Unable to register config group '%s': Too many configuration groups", key );
    80 }
    82 gboolean lxdream_find_config()
    83 {
    84     gboolean result = TRUE;
    85     char *home = getenv("HOME");
    86     if( lxdream_config_save_filename == NULL ) {
    87         /* For compatibility, look for the old ~/.lxdreamrc first. Use it if 
    88          * found, otherwise use the new ~/.lxdream/lxdreamrc.
    89          */
    90         lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    91         if( access(lxdream_config_save_filename, R_OK) != 0 ) {
    92             g_free(lxdream_config_save_filename);
    93             const char *user_path = get_user_data_path();
    94             lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
    95         }
    96     }
    97     if( lxdream_config_load_filename == NULL ) {
    98         char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
    99         if( access(lxdream_config_save_filename, R_OK) == 0 ) {
   100             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
   101             g_free(sysconfig);
   102         } else if( access( sysconfig, R_OK ) == 0 ) {
   103             lxdream_config_load_filename = sysconfig;
   104         } else {
   105             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
   106             g_free(sysconfig);
   107             result = FALSE;
   108         }	
   109     }
   110     return result;
   111 }
   113 void lxdream_set_config_filename( const gchar *filename )
   114 {
   115     if( lxdream_config_load_filename != NULL ) {
   116         g_free(lxdream_config_load_filename);
   117     }
   118     lxdream_config_load_filename = g_strdup(filename);
   119     if( lxdream_config_save_filename != NULL ) {
   120         g_free(lxdream_config_save_filename);
   121     }
   122     lxdream_config_save_filename = g_strdup(filename);
   123 }
   125 void lxdream_set_default_config( )
   126 {
   127     /* Construct platform dependent defaults */
   128     const gchar *user_path = get_user_data_path();
   129     global_group.params[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path ); 
   130     global_group.params[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path ); 
   131     global_group.params[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path ); 
   132     global_group.params[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path ); 
   133     global_group.params[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path ); 
   135     /* Copy defaults into main values */
   136     for( int i=0; lxdream_config_root[i] != NULL; i++ ) {
   137         struct lxdream_config_entry *param = lxdream_config_root[i]->params;
   138         if( param != NULL ) {
   139             while( param->key != NULL ) {
   140                 if( param->value != param->default_value ) {
   141                     if( param->value != NULL )
   142                         free( param->value );
   143                     param->value = (gchar *)param->default_value;
   144                 }
   145                 param++;
   146             }
   147         }
   148     }
   149     maple_detach_all();
   150 }
   152 const gchar *lxdream_get_config_value( lxdream_config_group_t group, int key )
   153 {
   154     return group->params[key].value;
   155 }
   157 gboolean lxdream_get_config_boolean_value( lxdream_config_group_t group, int key )
   158 {
   159     const gchar *value = lxdream_get_config_value(group, key);
   160     if( strcasecmp(value, "on") == 0 || strcasecmp(value, "true") == 0 ||
   161         strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 ) {
   162         return TRUE;
   163     } else {
   164         return FALSE;
   165     }
   166 }
   168 gboolean lxdream_set_config_boolean_value( lxdream_config_group_t group, int key, gboolean value )
   169 {
   170     return lxdream_set_config_value(group, key, value ? "on" : "off");
   171 }
   173 gboolean lxdream_set_config_value( lxdream_config_group_t group, int key, const gchar *value )
   174 {
   175     lxdream_config_entry_t param = &group->params[key];
   176     if( param->value != value &&
   177         (param->value == NULL || value == NULL || strcmp(param->value,value) != 0)  ) {
   179         gchar *new_value = g_strdup(value);
   181         /* If the group defines an on_change handler, it can block the change
   182          * (ie due to an invalid setting).
   183          */
   184         if( group->on_change == NULL ||
   185             group->on_change(group->data, group,key, param->value, new_value) ) {
   187             /* Don't free the default value, but otherwise need to release the
   188              * old value.
   189              */
   190             if( param->value != param->default_value && param->value != NULL ) {
   191                 free( param->value );
   192             }
   193             param->value = new_value;
   194         } else { /* on_change handler said no. */
   195             g_free(new_value);
   196             return FALSE;
   197         }
   198     }
   199     return TRUE;
   200 }
   202 const gchar *lxdream_get_global_config_value( int key )
   203 {
   204     return global_group.params[key].value;
   205 }
   207 void lxdream_set_global_config_value( int key, const gchar *value )
   208 {
   209     lxdream_set_config_value(&global_group, key, value);
   210 }
   212 GList *lxdream_get_global_config_list_value( int key )
   213 {
   214     GList *result = NULL;
   215     const gchar *str = lxdream_get_global_config_value( key );
   216     if( str != NULL ) {
   217         gchar **strv = g_strsplit(str, ":",0);
   218         int i;
   219         for( i=0; strv[i] != NULL; i++ ) {
   220             result = g_list_append( result, g_strdup(strv[i]) );
   221         }
   222         g_strfreev(strv);
   223     }
   224     return result;
   225 }
   227 void lxdream_set_global_config_list_value( int key, const GList *list )
   228 {
   229     if( list == NULL ) {
   230         lxdream_set_global_config_value( key, NULL );
   231     } else {
   232         const GList *ptr;
   233         int size = 0;
   235         for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
   236             size += strlen( (gchar *)ptr->data ) + 1;
   237         }
   238         char buf[size];
   239         strcpy( buf, (gchar *)list->data );
   240         for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
   241             strcat( buf, ":" );
   242             strcat( buf, (gchar *)ptr->data );
   243         }
   244         lxdream_set_global_config_value( key, buf );
   245     }
   246 }
   248 gchar *lxdream_get_global_config_path_value( int key )
   249 {
   250     const gchar *str = lxdream_get_global_config_value(key);
   251     if( str == NULL ) {
   252         return NULL;
   253     } else {
   254         return get_expanded_path(str);
   255     }
   256 }
   258 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
   259 {
   260     gchar *temp = get_escaped_path(value);
   261     lxdream_set_global_config_value(key,temp);
   262     g_free(temp);
   263     return lxdream_get_global_config_value(key);
   264 }
   266 struct lxdream_config_group * lxdream_get_config_group( int group )
   267 {
   268     return lxdream_config_root[group];
   269 }
   271 void lxdream_copy_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
   272 {
   273     int i;
   274     for( i=0; src->params[i].key != NULL; i++ ) {
   275         lxdream_set_config_value( dest, i, src->params[i].value );
   276     }
   277 }
   279 void lxdream_clone_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
   280 {
   281     int i;
   283     dest->key = src->key;
   284     dest->on_change = NULL;
   285     dest->key_binding = NULL;
   286     dest->data = NULL;
   287     for( i=0; src->params[i].key != NULL; i++ ) {
   288         dest->params[i].key = src->params[i].key;
   289         dest->params[i].label = src->params[i].label;
   290         dest->params[i].type = src->params[i].type;
   291         dest->params[i].tag = src->params[i].tag;
   292         dest->params[i].default_value = src->params[i].default_value;
   293         dest->params[i].value = NULL;
   294         lxdream_set_config_value( dest, i, src->params[i].value );
   295     }
   296     dest->params[i].key = NULL;
   297     dest->params[i].label = NULL;
   298 }
   300 gboolean lxdream_load_config( )
   301 {
   302     if( lxdream_config_load_filename == NULL ) {
   303         lxdream_find_config();
   304     }
   305     return lxdream_load_config_file(lxdream_config_load_filename);
   306 }
   308 gboolean lxdream_save_config( )
   309 {
   310     if( lxdream_config_save_filename == NULL ) {
   311         lxdream_find_config();
   312     }
   313     return lxdream_save_config_file(lxdream_config_save_filename);
   314 }
   316 gboolean lxdream_load_config_file( const gchar *filename )
   317 {
   318     FILE *f;
   319     gboolean result;
   321     if( access(filename, F_OK) != 0 ) {
   322         INFO( "Configuration file '%s' does not exist, creating from defaults" );
   323         lxdream_set_default_config();
   324         lxdream_save_config();
   325     }
   327     f = fopen(filename, "ro");
   328     if( f == NULL ) {
   329         ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   330         lxdream_set_default_config();
   331         return FALSE;
   332     }
   334     result = lxdream_load_config_stream( f );
   335     fclose(f);
   336     return result;
   337 }
   339 gboolean lxdream_load_config_stream( FILE *f )
   340 {
   342     char buf[512];
   343     int maple_device = -1, maple_subdevice = -1, i;
   344     struct lxdream_config_group *group = NULL;
   345     struct lxdream_config_group *top_group = NULL;
   346     maple_device_t device = NULL;
   347     lxdream_set_default_config();
   349     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   350         g_strstrip(buf);
   351         if( buf[0] == '#' )
   352             continue;
   353         if( *buf == '[' ) {
   354             char *p = strchr(buf, ']');
   355             if( p != NULL ) {
   356                 maple_device = maple_subdevice = -1;
   357                 *p = '\0';
   358                 g_strstrip(buf+1);
   359                 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
   360                     if( strcasecmp(lxdream_config_root[i]->key, buf+1) == 0 ) {
   361                         top_group = group = lxdream_config_root[i];
   362                         break;
   363                     }
   364                 }
   365             }
   366         } else if( group != NULL ) {
   367             char *value = strchr( buf, '=' );
   368             if( value != NULL ) {
   369                 struct lxdream_config_entry *param = group->params;
   370                 *value = '\0';
   371                 value++;
   372                 g_strstrip(buf);
   373                 g_strstrip(value);
   374                 if( top_group == &controllers_group ) {
   375                     if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   376                         maple_device = strtoul( buf+7, NULL, 0 );
   377                         if( maple_device < 0 || maple_device > 3 ) {
   378                             ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   379                             continue;
   380                         }
   381                         maple_subdevice = 0;
   382                         device = maple_new_device( value );
   383                         if( device == NULL ) {
   384                             ERROR( "Unrecognized device '%s'", value );
   385                         } else {
   386                             group = maple_get_device_config(device);
   387                             maple_attach_device( device, maple_device, maple_subdevice );
   388                         }
   389                         continue;
   390                     } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   391                         maple_subdevice = strtoul( buf+10, NULL, 0 );
   392                         if( maple_device == -1 ) {
   393                             ERROR( "Subdevice not allowed without primary device" );
   394                         } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   395                             ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   396                         } else if( (device = maple_new_device(value)) == NULL ) {
   397                             ERROR( "Unrecognized subdevice '%s'", value );
   398                         } else {
   399                             group = maple_get_device_config(device);
   400                             maple_attach_device( device, maple_device, maple_subdevice );
   401                         }
   402                         continue;
   403                     }
   404                 }
   405                 while( param->key != NULL ) {
   406                     if( strcasecmp( param->key, buf ) == 0 ) {
   407                         param->value = g_strdup(value);
   408                         break;
   409                     }
   410                     param++;
   411                 }
   412             }
   413         }
   414     }
   415     return TRUE;
   416 }
   418 gboolean lxdream_save_config_file( const gchar *filename )
   419 {
   420     FILE *f = fopen(filename, "wo");
   421     gboolean result;
   422     if( f == NULL ) {
   423         ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   424         return FALSE;
   425     }
   426     result = lxdream_save_config_stream(f);
   427     fclose(f);
   428     return TRUE;
   429 }    
   431 gboolean lxdream_save_config_stream( FILE *f )
   432 {
   433     int i;
   434     for( i=0; lxdream_config_root[i] != NULL; i++ ) {
   435         fprintf( f, "[%s]\n", lxdream_config_root[i]->key );
   437         if( lxdream_config_root[i] == &controllers_group ) {
   438             int i,j;
   439             for( i=0; i<4; i++ ) {
   440                 for( j=0; j<6; j++ ) {
   441                     maple_device_t dev = maple_get_device( i, j );
   442                     if( dev != NULL ) {
   443                         if( j == 0 )
   444                             fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   445                         else 
   446                             fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   447                         lxdream_config_group_t group = maple_get_device_config(dev);
   448                         if( group != NULL ) {
   449                             lxdream_config_entry_t entry = group->params;
   450                             while( entry->key != NULL ) {
   451                                 if( entry->value != NULL ) {
   452                                     fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   453                                 }
   454                                 entry++;
   455                             }
   456                         }
   457                     }
   458                 }
   459             }
   460         } else {
   461             struct lxdream_config_entry *entry = lxdream_config_root[i]->params;
   462             while( entry->key != NULL ) {
   463                 if( entry->value != NULL ) {
   464                     fprintf( f, "%s = %s\n", entry->key, entry->value );
   465                 }
   466                 entry++;
   467             }
   468         }
   469         fprintf( f, "\n" );
   470     }
   471     return TRUE;
   472 }
   474 void lxdream_make_config_dir( )
   475 {
   476     const char *user_path = get_user_data_path();
   477     struct stat st;
   479     if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
   480             (st.st_mode & S_IFDIR) != 0 ) {
   481         /* All good */
   482         return;
   483     }
   485     if( mkdir( user_path, 0777 ) != 0 ) {
   486         ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
   487         return;
   488     }
   490     char *vmupath = g_strdup_printf( "%s/vmu", user_path );
   491     mkdir( vmupath, 0777 );
   492     g_free( vmupath );
   494     char *savepath = g_strdup_printf( "%s/save", user_path );
   495     mkdir( savepath, 0777 );
   496     g_free( vmupath );
   497 }
.