From c44cca299fbea4196af125dfedd8e4d60f3ae0cc Mon Sep 17 00:00:00 2001 From: cnlohr Date: Sun, 18 Mar 2018 19:48:25 -0400 Subject: Almost at a config interface. --- src/survive.c | 57 ++++++++++++++++++++++---- src/survive_cal.c | 6 ++- src/survive_config.c | 109 ++++++++++++++++++++++++++++++++++++------------- src/survive_config.h | 10 ++--- src/survive_playback.c | 5 +-- src/survive_vive.c | 2 +- 6 files changed, 141 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/survive.c b/src/survive.c index 327b7f8..24bb7d3 100755 --- a/src/survive.c +++ b/src/survive.c @@ -106,7 +106,6 @@ void survive_verify_FLT_size(uint32_t user_size) { SurviveContext * survive_init_internal( int argc, char * const * argv ) { - #ifdef RUNTIME_SYMNUM if( !did_runtime_symnum ) { @@ -134,14 +133,58 @@ SurviveContext * survive_init_internal( int argc, char * const * argv ) ctx->lh_config = malloc( sizeof(config_group) * NUM_LIGHTHOUSES); //initdata - // ->argc ->argp init_config_group(ctx->global_config_values,10); init_config_group(ctx->temporary_config_values,20); init_config_group(&ctx->lh_config[0],10); init_config_group(&ctx->lh_config[1],10); - config_read(ctx, survive_config_reads( ctx, "configfile", "config.json" ) ); - ctx->activeLighthouses = survive_config_readi( ctx, "lighthousecount", 2 ); + //Process command-line parameters. + char * const * argvend = &argv[argc]; + char * const * av = argv+1; + int showhelp = 0; + for( ; av != argvend; av++ ) + { + if( (*av)[0] != '-' ) + showhelp = 1; + else + { + const char * vartoupdate = 0; + + switch( (*av)[1] ) + { + case '-': vartoupdate = &(*av)[2]; break; + case 'h': showhelp = 1; break; + case 'p': vartoupdate = "defaultposer"; break; + case 'l': vartoupdate = "lighthousecount"; break; + case 'c': vartoupdate = "configfile"; break; + default: + fprintf( stderr, "Error: unknown parameter %s\n", *av ); + showhelp = 1; + } + + if( vartoupdate ) + { + survive_configs( ctx, *av, SC_OVERRIDE, *(av+1) ); + av++; + } + } + } + if( showhelp ) + { + fprintf( stderr, "libsurvive - usage:\n" ); + fprintf( stderr, " --[parameter] [value] - sets parameter\n" ); + fprintf( stderr, " -h - shows help.\n" ); + fprintf( stderr, " -p [poser] - use a specific defaultposer.\n" ); + fprintf( stderr, " -l [lighthouse count] - use a specific number of lighthoses.\n" ); + fprintf( stderr, " -c [config file] - set config file\n" ); + fprintf( stderr, " -p [lighthouse count] - use a specific number of lighthoses.\n" ); + + //XXX: TODO: Should this just exit(-1)? + return 0; + } + + config_read(ctx, survive_configs( ctx, "configfile", SC_GET, "config.json" ) ); + ctx->activeLighthouses = survive_configi( ctx, "lighthousecount", SC_SETCONFIG, 2 ); config_read_lighthouse(ctx->lh_config, &(ctx->bsd[0]), 0); config_read_lighthouse(ctx->lh_config, &(ctx->bsd[1]), 1); @@ -173,7 +216,7 @@ int survive_startup( SurviveContext * ctx ) const char * DriverName; //const char * PreferredPoser = survive_config_reads(ctx->global_config_values, "defaultposer", "PoserDummy"); - const char * PreferredPoser = survive_config_reads( ctx, "defaultposer", "PoserTurveyTori"); + const char * PreferredPoser = survive_configs( ctx, "defaultposer", SC_SETCONFIG, "PoserTurveyTori"); PoserCB PreferredPoserCB = 0; const char * FirstPoser = 0; printf( "Available posers:\n" ); @@ -206,7 +249,7 @@ int survive_startup( SurviveContext * ctx ) } // saving the config extra to make sure that the user has a config file they can change. - config_save(ctx, survive_config_reads( ctx, "configfile", "config.json" ) ); + config_save(ctx, survive_configs( ctx, "configfile", SC_GET, "config.json" ) ); ctx->state = SURVIVE_RUNNING; @@ -367,7 +410,7 @@ void survive_close( SurviveContext * ctx ) } - config_save(ctx, survive_config_reads( ctx, "configfile", "config.json" ) ); + config_save(ctx, survive_configs( ctx, "configfile", SC_GET, "config.json" ) ); destroy_config_group(ctx->global_config_values); destroy_config_group(ctx->temporary_config_values); diff --git a/src/survive_cal.c b/src/survive_cal.c index 6f556f3..4b1df89 100755 --- a/src/survive_cal.c +++ b/src/survive_cal.c @@ -8,6 +8,7 @@ // to not include it at all on any stripped-down versions of libsurvive. // + #include "survive_cal.h" #include "survive_internal.h" #include "survive_reproject.h" @@ -178,8 +179,9 @@ void survive_cal_install( struct SurviveContext * ctx ) } const char * DriverName; -// const char * PreferredPoser = survive_config_reads(ctx, "configposer", "PoserCharlesSlow"); - const char * PreferredPoser = survive_config_reads(ctx, "configposer", "PoserTurveyTori"); +// const char * PreferredPoser = survive_configs(ctx, "configposer", "PoserCharlesSlow"); + const char * PreferredPoser = survive_configs(ctx, "configposer", SC_SETCONFIG, "PoserTurveyTori"); + PoserCB PreferredPoserCB = 0; const char * FirstPoser = 0; printf( "Available posers:\n" ); diff --git a/src/survive_config.c b/src/survive_config.c index 4ef2994..1a64828 100644 --- a/src/survive_config.c +++ b/src/survive_config.c @@ -181,34 +181,6 @@ FLT config_read_float(config_group *cg, const char *tag, const FLT def) { return config_set_float(cg, tag, def); } -static config_entry * sc_search(SurviveContext * ctx, const char *tag ) -{ - config_entry *cv = find_config_entry(ctx->temporary_config_values, tag); - if( !cv ) cv = find_config_entry(ctx->global_config_values, tag); - return cv; -} - -FLT survive_config_readf( SurviveContext * ctx, const char *tag, FLT def ) -{ - config_entry * cv = sc_search( ctx, tag ); - if( !cv ) return def; - return cv->numeric.f; -} - -uint32_t survive_config_readi( SurviveContext * ctx, const char *tag, uint32_t def ) -{ - config_entry * cv = sc_search( ctx, tag ); - if( !cv ) return def; - return cv->numeric.i; -} - -const char * survive_config_reads( SurviveContext * ctx, const char *tag, const char *def ) -{ - config_entry * cv = sc_search( ctx, tag ); - if( !cv ) return def; - return cv->data; -} - // TODO: Do something better than this: @@ -471,3 +443,84 @@ void config_read(SurviveContext* sctx, const char* path) { json_tag_value = NULL; } + + + +static config_entry * sc_search(SurviveContext * ctx, const char *tag ) +{ + config_entry *cv = find_config_entry(ctx->temporary_config_values, tag); + if( !cv ) cv = find_config_entry(ctx->global_config_values, tag); + return cv; +} + + + +FLT survive_configf( SurviveContext * ctx, const char *tag, char flags, FLT def ) +{ + if( !(flags & SC_OVERRIDE) ) + { + config_entry * cv = sc_search( ctx, tag ); + if( cv ) + return cv->numeric.f; + } + + //If override is flagged, or, we can't find the variable, ,continue on. + if( flags & SC_SETCONFIG ) + { + config_set_float( ctx->temporary_config_values, tag, def ); + config_set_float( ctx->global_config_values, tag, def ); + } + else if( flags & SC_SET ) + { + config_set_float( ctx->temporary_config_values, tag, def ); + } + + return def; +} + +uint32_t survive_configi( SurviveContext * ctx, const char *tag, char flags, uint32_t def ) +{ + if( !(flags & SC_OVERRIDE) ) + { + config_entry * cv = sc_search( ctx, tag ); + if( cv ) + return cv->numeric.i; + } + + //If override is flagged, or, we can't find the variable, ,continue on. + if( flags & SC_SETCONFIG ) + { + config_set_uint32( ctx->temporary_config_values, tag, def ); + config_set_uint32( ctx->global_config_values, tag, def ); + } + else if( flags & SC_SET ) + { + config_set_uint32( ctx->temporary_config_values, tag, def ); + } + + return def; +} + +const char * survive_configs( SurviveContext * ctx, const char *tag, char flags, const char *def ) +{ + if( !(flags & SC_OVERRIDE) ) + { + config_entry * cv = sc_search( ctx, tag ); + if( cv ) + return cv->data; + } + + //If override is flagged, or, we can't find the variable, ,continue on. + if( flags & SC_SETCONFIG ) + { + config_set_str( ctx->temporary_config_values, tag, def ); + config_set_str( ctx->global_config_values, tag, def ); + } + else if( flags & SC_SET ) + { + config_set_str( ctx->temporary_config_values, tag, def ); + } + + return def; +} + diff --git a/src/survive_config.h b/src/survive_config.h index 1ae124d..23d80c8 100644 --- a/src/survive_config.h +++ b/src/survive_config.h @@ -1,5 +1,7 @@ // (C) 2017 <>< Joshua Allen, Under MIT/x11 License. - +// +// This header is for handling internal parameter values. Most accesses should be done through functions like survive_config +// #ifndef _SURVIVE_CONFIG_H #define _SURVIVE_CONFIG_H @@ -58,10 +60,4 @@ uint16_t config_read_float_array(config_group *cg, const char *tag, FLT* values, uint32_t config_read_uint32(config_group *cg, const char *tag, const uint32_t def); const char* config_read_str(config_group *cg, const char *tag, const char *def); -//These functions search both the stored-general and temporary sections for a parameter and return it. -//FLT survive_config_readf( SurviveContext * ctx, const char *tag, FLT def ); -//uint32_t survive_config_readi( SurviveContext * ctx, const char *tag, uint32_t def ); -//const char * survive_config_reads( SurviveContext * ctx, const char *tag, const char *def ); -//They're actually defined in survive.h for users as well. - #endif diff --git a/src/survive_playback.c b/src/survive_playback.c index df9fcaa..014542b 100644 --- a/src/survive_playback.c +++ b/src/survive_playback.c @@ -203,7 +203,7 @@ static int LoadConfig(SurvivePlaybackData *sv, SurviveObject *so) { } int DriverRegPlayback(SurviveContext *ctx) { - const char *playback_file = config_read_str(ctx->global_config_values, "PlaybackFile", ""); + const char *playback_file = survive_configs(ctx, "playbackfile", SC_SETCONFIG, ""); if (strlen(playback_file) == 0) { return 0; @@ -212,8 +212,7 @@ int DriverRegPlayback(SurviveContext *ctx) { SurvivePlaybackData *sp = calloc(1, sizeof(SurvivePlaybackData)); sp->ctx = ctx; sp->playback_dir = playback_file; - sp->time_factor = - config_read_float(ctx->global_config_values, "PlaybackFactor", 1.); + sp->time_factor = survive_configf(ctx, "playbackfactor", SC_SETCONFIG, 1.f); printf("%s\n", playback_file); diff --git a/src/survive_vive.c b/src/survive_vive.c index 4411efb..47af701 100755 --- a/src/survive_vive.c +++ b/src/survive_vive.c @@ -1712,7 +1712,7 @@ void init_SurviveObject(SurviveObject* so) { int DriverRegHTCVive( SurviveContext * ctx ) { - const char *playback_dir = config_read_str(ctx->global_config_values, "PlaybackFile", ""); + const char *playback_dir = survive_configs(ctx, "playbackfile", SC_SETCONFIG, ""); if(strlen(playback_dir) != 0) { SV_INFO("Playback is active; disabling USB driver"); return 0; -- cgit v1.2.3