91eed5d257ebe9299dbd459e6d2fff3b8542b551
[vrtris] / src / opt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "optcfg.h"
5 #include "opt.h"
6
7
8 struct options def_opt = { 1024, 768, 0 };
9
10 enum {
11         OPTCFG_SIZE,
12         OPTCFG_VR,
13         OPTCFG_FULLSCREEN,
14         OPTCFG_WINDOWED,
15         OPTCFG_HELP
16 };
17
18 static struct optcfg_option options[] = {
19         // short, long, id, desc
20         {'s', "size", OPTCFG_SIZE, "window size (WxH)"},
21         {0, "vr", OPTCFG_VR, "enable VR mode"},
22         {'f', "fullscreen", OPTCFG_FULLSCREEN, "run in fullscreen mode"},
23         {'w', "windowed", OPTCFG_WINDOWED, "run in windowed mode"},
24         {'h', "help", OPTCFG_HELP, "print usage and exit"},
25         OPTCFG_OPTIONS_END
26 };
27
28 static int opt_handler(struct optcfg *oc, int opt, void *cls);
29 static int arg_handler(struct optcfg *oc, const char *arg, void *cls);
30
31 static char *argv0;
32
33 int init_options(int argc, char **argv, const char *cfgfile)
34 {
35         struct optcfg *oc;
36
37         /* default options */
38         opt = def_opt;
39
40         argv0 = argv[0];
41
42         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 -1;
54         }
55
56         optcfg_destroy(oc);
57         return 0;
58 }
59
60 static int is_enabled(struct optcfg *oc)
61 {
62         int res;
63         optcfg_enabled_value(oc, &res);
64         return res != 0;
65 }
66
67 static int opt_handler(struct optcfg *oc, int optid, void *cls)
68 {
69         switch(optid) {
70         case OPTCFG_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 OPTCFG_VR:
81                 if(is_enabled(oc)) {
82                         opt.flags |= OPT_VR;
83                 } else {
84                         opt.flags &= ~OPT_VR;
85                 }
86                 break;
87
88         case OPTCFG_FULLSCREEN:
89                 if(is_enabled(oc)) {
90                         opt.flags |= OPT_FULLSCREEN;
91                 } else {
92                         opt.flags &= ~OPT_FULLSCREEN;
93                 }
94                 break;
95
96         case OPTCFG_WINDOWED:
97                 if(is_enabled(oc)) {
98                         opt.flags &= ~OPT_FULLSCREEN;
99                 } else {
100                         opt.flags |= OPT_FULLSCREEN;
101                 }
102                 break;
103
104         case OPTCFG_HELP:
105                 printf("Usage: %s [options]\nOptions:\n", argv0);
106                 optcfg_print_options(oc);
107                 exit(0);
108         }
109         return 0;
110 }
111
112 static int arg_handler(struct optcfg *oc, const char *arg, void *cls)
113 {
114         fprintf(stderr, "unexpected argument: %s\n", arg);
115         return -1;
116 }