Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1296:30ecee61f811
prev1144:00dd49743974
author nkeynes
date Sat Jan 26 14:00:48 2013 +1000 (9 years ago)
permissions -rw-r--r--
last change Change glib includes to #include <glib.h> rather than the individual
headers, as recent glib versions are breaking on this
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.h>
    25 #include <sys/types.h>
    26 #include <sys/stat.h>
    27 #include "dreamcast.h"
    28 #include "config.h"
    29 #include "lxpaths.h"
    30 #include "maple/maple.h"
    32 #define MAX_ROOT_GROUPS 16
    34 extern struct lxdream_config_group hotkeys_group;
    35 extern struct lxdream_config_group serial_group;
    37 gboolean lxdream_load_config_file( const gchar *filename );
    38 gboolean lxdream_save_config_file( const gchar *filename );
    39 gboolean lxdream_load_config_stream( FILE *f );
    40 gboolean lxdream_save_config_stream( FILE *f );
    42 static struct lxdream_config_group global_group =
    43     { "global", dreamcast_config_changed, NULL, NULL,
    44        {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, NULL },
    45         { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, NULL },
    46         { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
    47         { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, NULL },
    48         { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, NULL },
    49         { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, NULL },
    50         { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
    51         { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
    52         { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
    53         { "quick state", NULL, CONFIG_TYPE_INTEGER, "0" },
    54         { NULL, CONFIG_TYPE_NONE }} };
    56 /**
    57  * Dummy group for controllers (handled specially)
    58  */
    59 static struct lxdream_config_group controllers_group =
    60     { "controllers", NULL, NULL, NULL, {{NULL, CONFIG_TYPE_NONE}} };
    62 struct lxdream_config_group *lxdream_config_root[MAX_ROOT_GROUPS+1] = 
    63        { &global_group, &controllers_group, &hotkeys_group, &serial_group, NULL };
    65 static gchar *lxdream_config_load_filename = NULL;
    66 static gchar *lxdream_config_save_filename = NULL;
    68 void lxdream_register_config_group( const gchar *key, lxdream_config_group_t group )
    69 {
    70     int i;
    71     for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
    72         if( lxdream_config_root[i] == NULL ) {
    73             lxdream_config_root[i] = group;
    74             lxdream_config_root[i+1] = NULL;
    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         /* For compatibility, look for the old ~/.lxdreamrc first. Use it if 
    87          * found, otherwise use the new ~/.lxdream/lxdreamrc.
    88          */
    89         lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    90         if( access(lxdream_config_save_filename, R_OK) != 0 ) {
    91             g_free(lxdream_config_save_filename);
    92             const char *user_path = get_user_data_path();
    93             lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
    94         }
    95     }
    96     if( lxdream_config_load_filename == NULL ) {
    97         char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
    98         if( access(lxdream_config_save_filename, R_OK) == 0 ) {
    99             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
   100             g_free(sysconfig);
   101         } else if( access( sysconfig, R_OK ) == 0 ) {
   102             lxdream_config_load_filename = sysconfig;
   103         } else {
   104             lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
   105             g_free(sysconfig);
   106             result = FALSE;
   107         }	
   108     }
   109     return result;
   110 }
   112 void lxdream_set_config_filename( const gchar *filename )
   113 {
   114     if( lxdream_config_load_filename != NULL ) {
   115         g_free(lxdream_config_load_filename);
   116     }
   117     lxdream_config_load_filename = g_strdup(filename);
   118     if( lxdream_config_save_filename != NULL ) {
   119         g_free(lxdream_config_save_filename);
   120     }
   121     lxdream_config_save_filename = g_strdup(filename);
   122 }
   124 void lxdream_set_default_config( )
   125 {
   126     /* Construct platform dependent defaults */
   127     const gchar *user_path = get_user_data_path();
   128     global_group.params[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path ); 
   129     global_group.params[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path ); 
   130     global_group.params[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path ); 
   131     global_group.params[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path ); 
   132     global_group.params[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path ); 
   134     /* Copy defaults into main values */
   135     for( int i=0; lxdream_config_root[i] != NULL; i++ ) {
   136         struct lxdream_config_entry *param = lxdream_config_root[i]->params;
   137         if( param != NULL ) {
   138             while( param->key != NULL ) {
   139                 if( param->value != param->default_value ) {
   140                     if( param->value != NULL )
   141                         free( param->value );
   142                     param->value = (gchar *)param->default_value;
   143                 }
   144                 param++;
   145             }
   146         }
   147     }
   148     maple_detach_all();
   149 }
   151 const gchar *lxdream_get_config_value( lxdream_config_group_t group, int key )
   152 {
   153     return group->params[key].value;
   154 }
   156 gboolean lxdream_get_config_boolean_value( lxdream_config_group_t group, int key )
   157 {
   158     const gchar *value = lxdream_get_config_value(group, key);
   159     if( strcasecmp(value, "on") == 0 || strcasecmp(value, "true") == 0 ||
   160         strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 ) {
   161         return TRUE;
   162     } else {
   163         return FALSE;
   164     }
   165 }
   167 gboolean lxdream_set_config_boolean_value( lxdream_config_group_t group, int key, gboolean value )
   168 {
   169     return lxdream_set_config_value(group, key, value ? "on" : "off");
   170 }
   172 gboolean lxdream_set_config_value( lxdream_config_group_t group, int key, const gchar *value )
   173 {
   174     lxdream_config_entry_t param = &group->params[key];
   175     if( param->value != value &&
   176         (param->value == NULL || value == NULL || strcmp(param->value,value) != 0)  ) {
   178         gchar *new_value = g_strdup(value);
   180         /* If the group defines an on_change handler, it can block the change
   181          * (ie due to an invalid setting).
   182          */
   183         if( group->on_change == NULL ||
   184             group->on_change(group->data, group,key, param->value, new_value) ) {
   186             /* Don't free the default value, but otherwise need to release the
   187              * old value.
   188              */
   189             if( param->value != param->default_value && param->value != NULL ) {
   190                 free( param->value );
   191             }
   192             param->value = new_value;
   193         } else { /* on_change handler said no. */
   194             g_free(new_value);
   195             return FALSE;
   196         }
   197     }
   198     return TRUE;
   199 }
   201 const gchar *lxdream_get_global_config_value( int key )
   202 {
   203     return global_group.params[key].value;
   204 }
   206 void lxdream_set_global_config_value( int key, const gchar *value )
   207 {
   208     lxdream_set_config_value(&global_group, key, value);
   209 }
   211 GList *lxdream_get_global_config_list_value( int key )
   212 {
   213     GList *result = NULL;
   214     const gchar *str = lxdream_get_global_config_value( key );
   215     if( str != NULL ) {
   216         gchar **strv = g_strsplit(str, ":",0);
   217         int i;
   218         for( i=0; strv[i] != NULL; i++ ) {
   219             result = g_list_append( result, g_strdup(strv[i]) );
   220         }
   221         g_strfreev(strv);
   222     }
   223     return result;
   224 }
   226 void lxdream_set_global_config_list_value( int key, const GList *list )
   227 {
   228     if( list == NULL ) {
   229         lxdream_set_global_config_value( key, NULL );
   230     } else {
   231         const GList *ptr;
   232         int size = 0;
   234         for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
   235             size += strlen( (gchar *)ptr->data ) + 1;
   236         }
   237         char buf[size];
   238         strcpy( buf, (gchar *)list->data );
   239         for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
   240             strcat( buf, ":" );
   241             strcat( buf, (gchar *)ptr->data );
   242         }
   243         lxdream_set_global_config_value( key, buf );
   244     }
   245 }
   247 gchar *lxdream_get_global_config_path_value( int key )
   248 {
   249     const gchar *str = lxdream_get_global_config_value(key);
   250     if( str == NULL ) {
   251         return NULL;
   252     } else {
   253         return get_expanded_path(str);
   254     }
   255 }
   257 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
   258 {
   259     gchar *temp = get_escaped_path(value);
   260     lxdream_set_global_config_value(key,temp);
   261     g_free(temp);
   262     return lxdream_get_global_config_value(key);
   263 }
   265 struct lxdream_config_group * lxdream_get_config_group( int group )
   266 {
   267     return lxdream_config_root[group];
   268 }
   270 void lxdream_copy_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
   271 {
   272     int i;
   273     for( i=0; src->params[i].key != NULL; i++ ) {
   274         lxdream_set_config_value( dest, i, src->params[i].value );
   275     }
   276 }
   278 void lxdream_clone_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
   279 {
   280     int i;
   282     dest->key = src->key;
   283     dest->on_change = NULL;
   284     dest->key_binding = NULL;
   285     dest->data = NULL;
   286     for( i=0; src->params[i].key != NULL; i++ ) {
   287         dest->params[i].key = src->params[i].key;
   288         dest->params[i].label = src->params[i].label;
   289         dest->params[i].type = src->params[i].type;
   290         dest->params[i].tag = src->params[i].tag;
   291         dest->params[i].default_value = src->params[i].default_value;
   292         dest->params[i].value = NULL;
   293         lxdream_set_config_value( dest, i, src->params[i].value );
   294     }
   295     dest->params[i].key = NULL;
   296     dest->params[i].label = NULL;
   297 }
   299 gboolean lxdream_load_config( )
   300 {
   301     if( lxdream_config_load_filename == NULL ) {
   302         lxdream_find_config();
   303     }
   304     return lxdream_load_config_file(lxdream_config_load_filename);
   305 }
   307 gboolean lxdream_save_config( )
   308 {
   309     if( lxdream_config_save_filename == NULL ) {
   310         lxdream_find_config();
   311     }
   312     return lxdream_save_config_file(lxdream_config_save_filename);
   313 }
   315 gboolean lxdream_load_config_file( const gchar *filename )
   316 {
   317     FILE *f;
   318     gboolean result;
   320     if( access(filename, F_OK) != 0 ) {
   321         INFO( "Configuration file '%s' does not exist, creating from defaults" );
   322         lxdream_set_default_config();
   323         lxdream_save_config();
   324     }
   326     f = fopen(filename, "ro");
   327     if( f == NULL ) {
   328         ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   329         lxdream_set_default_config();
   330         return FALSE;
   331     }
   333     result = lxdream_load_config_stream( f );
   334     fclose(f);
   335     return result;
   336 }
   338 gboolean lxdream_load_config_stream( FILE *f )
   339 {
   341     char buf[512];
   342     int maple_device = -1, maple_subdevice = -1, i;
   343     struct lxdream_config_group *group = NULL;
   344     struct lxdream_config_group *top_group = NULL;
   345     maple_device_t device = NULL;
   346     lxdream_set_default_config();
   348     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   349         g_strstrip(buf);
   350         if( buf[0] == '#' )
   351             continue;
   352         if( *buf == '[' ) {
   353             char *p = strchr(buf, ']');
   354             if( p != NULL ) {
   355                 maple_device = maple_subdevice = -1;
   356                 *p = '\0';
   357                 g_strstrip(buf+1);
   358                 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
   359                     if( strcasecmp(lxdream_config_root[i]->key, buf+1) == 0 ) {
   360                         top_group = group = lxdream_config_root[i];
   361                         break;
   362                     }
   363                 }
   364             }
   365         } else if( group != NULL ) {
   366             char *value = strchr( buf, '=' );
   367             if( value != NULL ) {
   368                 struct lxdream_config_entry *param = group->params;
   369                 *value = '\0';
   370                 value++;
   371                 g_strstrip(buf);
   372                 g_strstrip(value);
   373                 if( top_group == &controllers_group ) {
   374                     if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   375                         maple_device = strtoul( buf+7, NULL, 0 );
   376                         if( maple_device < 0 || maple_device > 3 ) {
   377                             ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   378                             continue;
   379                         }
   380                         maple_subdevice = 0;
   381                         device = maple_new_device( value );
   382                         if( device == NULL ) {
   383                             ERROR( "Unrecognized device '%s'", value );
   384                         } else {
   385                             group = maple_get_device_config(device);
   386                             maple_attach_device( device, maple_device, maple_subdevice );
   387                         }
   388                         continue;
   389                     } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   390                         maple_subdevice = strtoul( buf+10, NULL, 0 );
   391                         if( maple_device == -1 ) {
   392                             ERROR( "Subdevice not allowed without primary device" );
   393                         } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   394                             ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   395                         } else if( (device = maple_new_device(value)) == NULL ) {
   396                             ERROR( "Unrecognized subdevice '%s'", value );
   397                         } else {
   398                             group = maple_get_device_config(device);
   399                             maple_attach_device( device, maple_device, maple_subdevice );
   400                         }
   401                         continue;
   402                     }
   403                 }
   404                 while( param->key != NULL ) {
   405                     if( strcasecmp( param->key, buf ) == 0 ) {
   406                         param->value = g_strdup(value);
   407                         break;
   408                     }
   409                     param++;
   410                 }
   411             }
   412         }
   413     }
   414     return TRUE;
   415 }
   417 gboolean lxdream_save_config_file( const gchar *filename )
   418 {
   419     FILE *f = fopen(filename, "wo");
   420     gboolean result;
   421     if( f == NULL ) {
   422         ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   423         return FALSE;
   424     }
   425     result = lxdream_save_config_stream(f);
   426     fclose(f);
   427     return TRUE;
   428 }    
   430 gboolean lxdream_save_config_stream( FILE *f )
   431 {
   432     int i;
   433     for( i=0; lxdream_config_root[i] != NULL; i++ ) {
   434         fprintf( f, "[%s]\n", lxdream_config_root[i]->key );
   436         if( lxdream_config_root[i] == &controllers_group ) {
   437             int i,j;
   438             for( i=0; i<4; i++ ) {
   439                 for( j=0; j<6; j++ ) {
   440                     maple_device_t dev = maple_get_device( i, j );
   441                     if( dev != NULL ) {
   442                         if( j == 0 )
   443                             fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   444                         else 
   445                             fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   446                         lxdream_config_group_t group = maple_get_device_config(dev);
   447                         if( group != NULL ) {
   448                             lxdream_config_entry_t entry = group->params;
   449                             while( entry->key != NULL ) {
   450                                 if( entry->value != NULL ) {
   451                                     fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   452                                 }
   453                                 entry++;
   454                             }
   455                         }
   456                     }
   457                 }
   458             }
   459         } else {
   460             struct lxdream_config_entry *entry = lxdream_config_root[i]->params;
   461             while( entry->key != NULL ) {
   462                 if( entry->value != NULL ) {
   463                     fprintf( f, "%s = %s\n", entry->key, entry->value );
   464                 }
   465                 entry++;
   466             }
   467         }
   468         fprintf( f, "\n" );
   469     }
   470     return TRUE;
   471 }
   473 void lxdream_make_config_dir( )
   474 {
   475     const char *user_path = get_user_data_path();
   476     struct stat st;
   478     if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
   479             (st.st_mode & S_IFDIR) != 0 ) {
   480         /* All good */
   481         return;
   482     }
   484     if( mkdir( user_path, 0777 ) != 0 ) {
   485         ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
   486         return;
   487     }
   489     char *vmupath = g_strdup_printf( "%s/vmu", user_path );
   490     mkdir( vmupath, 0777 );
   491     g_free( vmupath );
   493     char *savepath = g_strdup_printf( "%s/save", user_path );
   494     mkdir( savepath, 0777 );
   495     g_free( vmupath );
   496 }
.