X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=vrfileman;a=blobdiff_plain;f=src%2Fopt.cc;fp=src%2Fopt.cc;h=0b9914983c5de853b38120cc121ee6cc834a1abf;hp=0000000000000000000000000000000000000000;hb=b7ea5b1ae3f0f0a5eba13b49e303b49876ee209e;hpb=65fa35533b5f8e26b57a31d5972a7649d71ad684 diff --git a/src/opt.cc b/src/opt.cc new file mode 100644 index 0000000..0b99149 --- /dev/null +++ b/src/opt.cc @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include "opt.h" + +Options opt; + +enum { + OPT_VR, + OPT_SRGB, + OPT_FULLSCREEN, + OPT_WINDOWED, + OPT_HELP +}; + +static optcfg_option options[] = { + // short, long, id, desc + {0, "vr", OPT_VR, "enable VR mode"}, + {0, "srgb", OPT_SRGB, "use linear color space"}, + {'f', "fullscreen", OPT_FULLSCREEN, "run in fullscreen mode"}, + {'w', "windowed", OPT_WINDOWED, "run in windowed mode"}, + {'h', "help", OPT_HELP, "print usage and exit"}, + OPTCFG_OPTIONS_END +}; + +static int opt_handler(optcfg *oc, int opt, void *cls); + +bool init_options(int argc, char **argv, const char *cfgfile) +{ + // default options + memset(&opt, 0, sizeof opt); + opt.srgb = true; + + optcfg *oc = optcfg_init(options); + optcfg_set_opt_callback(oc, opt_handler, 0); + + if(cfgfile && optcfg_parse_config_file(oc, cfgfile) == -1) { + optcfg_destroy(oc); + return false; + } + + if(argv && optcfg_parse_args(oc, argc, argv) == -1) { + fprintf(stderr, "invalid option\n"); + optcfg_destroy(oc); + return false; + } + + optcfg_destroy(oc); + return true; +} + +static bool is_enabled(optcfg *oc) +{ + int res; + optcfg_enabled_value(oc, &res); + return res != 0; +} + +static int opt_handler(optcfg *oc, int optid, void *cls) +{ + switch(optid) { + case OPT_VR: + opt.vr = is_enabled(oc); + break; + + case OPT_SRGB: + opt.srgb = is_enabled(oc); + break; + + case OPT_FULLSCREEN: + opt.fullscreen = is_enabled(oc); + break; + + case OPT_WINDOWED: + opt.fullscreen = !is_enabled(oc); + break; + + case OPT_HELP: + printf("Usage: vrfileman [options]\nOptions:\n"); + optcfg_print_options(oc); + exit(0); + } + return true; +}