0b9914983c5de853b38120cc121ee6cc834a1abf
[vrfileman] / src / opt.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <optcfg.h>
5 #include "opt.h"
6
7 Options opt;
8
9 enum {
10         OPT_VR,
11         OPT_SRGB,
12         OPT_FULLSCREEN,
13         OPT_WINDOWED,
14         OPT_HELP
15 };
16
17 static optcfg_option options[] = {
18         // short, long, id, desc
19         {0, "vr", OPT_VR, "enable VR mode"},
20         {0, "srgb", OPT_SRGB, "use linear color space"},
21         {'f', "fullscreen", OPT_FULLSCREEN, "run in fullscreen mode"},
22         {'w', "windowed", OPT_WINDOWED, "run in windowed mode"},
23         {'h', "help", OPT_HELP, "print usage and exit"},
24         OPTCFG_OPTIONS_END
25 };
26
27 static int opt_handler(optcfg *oc, int opt, void *cls);
28
29 bool init_options(int argc, char **argv, const char *cfgfile)
30 {
31         // default options
32         memset(&opt, 0, sizeof opt);
33         opt.srgb = true;
34
35         optcfg *oc = optcfg_init(options);
36         optcfg_set_opt_callback(oc, opt_handler, 0);
37
38         if(cfgfile && optcfg_parse_config_file(oc, cfgfile) == -1) {
39                 optcfg_destroy(oc);
40                 return false;
41         }
42
43         if(argv && optcfg_parse_args(oc, argc, argv) == -1) {
44                 fprintf(stderr, "invalid option\n");
45                 optcfg_destroy(oc);
46                 return false;
47         }
48
49         optcfg_destroy(oc);
50         return true;
51 }
52
53 static bool is_enabled(optcfg *oc)
54 {
55         int res;
56         optcfg_enabled_value(oc, &res);
57         return res != 0;
58 }
59
60 static int opt_handler(optcfg *oc, int optid, void *cls)
61 {
62         switch(optid) {
63         case OPT_VR:
64                 opt.vr = is_enabled(oc);
65                 break;
66
67         case OPT_SRGB:
68                 opt.srgb = is_enabled(oc);
69                 break;
70
71         case OPT_FULLSCREEN:
72                 opt.fullscreen = is_enabled(oc);
73                 break;
74
75         case OPT_WINDOWED:
76                 opt.fullscreen = !is_enabled(oc);
77                 break;
78
79         case OPT_HELP:
80                 printf("Usage: vrfileman [options]\nOptions:\n");
81                 optcfg_print_options(oc);
82                 exit(0);
83         }
84         return true;
85 }