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