18d73f2f3bd5c743054ca3301cfb661c1bc9eb94
[laserbrain_demo] / 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 Options def_opt = {
10         1280, 800,
11         false,  // vr
12         false,  // fullscreen
13         0               // scene file
14 };
15
16 enum {
17         OPT_SIZE,
18         OPT_VR,
19         OPT_FULLSCREEN,
20         OPT_WINDOWED,
21         OPT_SCENEFILE,
22         OPT_HELP
23 };
24
25 static optcfg_option options[] = {
26         // short, long, id, desc
27         {'s', "size", OPT_SIZE, "window size (WxH)"},
28         {0, "vr", OPT_VR, "enable VR mode"},
29         {'f', "fullscreen", OPT_FULLSCREEN, "run in fullscreen mode"},
30         {'w', "windowed", OPT_WINDOWED, "run in windowed mode"},
31         {0, "scene", OPT_SCENEFILE, "scene file to open"},
32         {'h', "help", OPT_HELP, "print usage and exit"},
33         OPTCFG_OPTIONS_END
34 };
35
36 static int opt_handler(optcfg *oc, int opt, void *cls);
37 static int arg_handler(optcfg *oc, const char *arg, void *cls);
38
39 bool init_options(int argc, char **argv, const char *cfgfile)
40 {
41         // default options
42         opt = def_opt;
43
44         optcfg *oc = optcfg_init(options);
45         optcfg_set_opt_callback(oc, opt_handler, 0);
46         optcfg_set_arg_callback(oc, arg_handler, 0);
47
48         if(cfgfile) {
49                 optcfg_parse_config_file(oc, cfgfile);
50         }
51
52         if(argv && optcfg_parse_args(oc, argc, argv) == -1) {
53                 fprintf(stderr, "invalid option\n");
54                 optcfg_destroy(oc);
55                 return false;
56         }
57
58         optcfg_destroy(oc);
59         return true;
60 }
61
62 static bool is_enabled(optcfg *oc)
63 {
64         int res;
65         optcfg_enabled_value(oc, &res);
66         return res != 0;
67 }
68
69 static int opt_handler(optcfg *oc, int optid, void *cls)
70 {
71         switch(optid) {
72         case OPT_SIZE:
73                 {
74                         char *valstr = optcfg_next_value(oc);
75                         if(!valstr || sscanf(valstr, "%dx%d", &opt.width, &opt.height) != 2) {
76                                 fprintf(stderr, "size must be in the form: WIDTHxHEIGHT\n");
77                                 return -1;
78                         }
79                 }
80                 break;
81
82         case OPT_VR:
83                 opt.vr = is_enabled(oc);
84                 break;
85
86         case OPT_FULLSCREEN:
87                 opt.fullscreen = is_enabled(oc);
88                 break;
89
90         case OPT_WINDOWED:
91                 opt.fullscreen = !is_enabled(oc);
92                 break;
93
94         case OPT_SCENEFILE:
95                 opt.scenefile = strdup(optcfg_next_value(oc));
96                 break;
97
98         case OPT_HELP:
99                 printf("Usage: vrfileman [options]\nOptions:\n");
100                 optcfg_print_options(oc);
101                 exit(0);
102         }
103         return 0;
104 }
105
106 static int arg_handler(optcfg *oc, const char *arg, void *cls)
107 {
108         if(opt.scenefile) {
109                 fprintf(stderr, "unexpected argument: %s\n", arg);
110                 return -1;
111         }
112         opt.scenefile = arg;
113         return 0;
114 }