build-time config option for VR mode
[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, "game" };
9
10 enum {
11         OPTCFG_SIZE,
12 #ifdef BUILD_VR
13         OPTCFG_VR,
14 #endif
15         OPTCFG_FULLSCREEN,
16         OPTCFG_WINDOWED,
17         OPTCFG_SCREEN,
18         OPTCFG_HELP
19 };
20
21 static struct optcfg_option options[] = {
22         // short, long, id, desc
23         {'s', "size", OPTCFG_SIZE, "window size (WxH)"},
24 #ifdef BUILD_VR
25         {0, "vr", OPTCFG_VR, "enable VR mode"},
26 #endif
27         {'f', "fullscreen", OPTCFG_FULLSCREEN, "run in fullscreen mode"},
28         {'w', "windowed", OPTCFG_WINDOWED, "run in windowed mode"},
29         {0, "screen", OPTCFG_SCREEN, "select starting screen"},
30         {'h', "help", OPTCFG_HELP, "print usage and exit"},
31         OPTCFG_OPTIONS_END
32 };
33
34 static int opt_handler(struct optcfg *oc, int opt, void *cls);
35 static int arg_handler(struct optcfg *oc, const char *arg, void *cls);
36
37 static char *argv0;
38
39 int init_options(int argc, char **argv, const char *cfgfile)
40 {
41         struct optcfg *oc;
42
43         /* default options */
44         opt = def_opt;
45         if(!(opt.start_scr = malloc(strlen(def_opt.start_scr) + 1))) {
46                 perror("failed to allocate memory");
47                 return -1;
48         }
49         strcpy(opt.start_scr, def_opt.start_scr);
50
51         argv0 = argv[0];
52
53         oc = optcfg_init(options);
54         optcfg_set_opt_callback(oc, opt_handler, 0);
55         optcfg_set_arg_callback(oc, arg_handler, 0);
56
57         if(cfgfile) {
58                 optcfg_parse_config_file(oc, cfgfile);
59         }
60
61         if(argv && optcfg_parse_args(oc, argc, argv) == -1) {
62                 fprintf(stderr, "invalid option\n");
63                 optcfg_destroy(oc);
64                 return -1;
65         }
66
67         optcfg_destroy(oc);
68         return 0;
69 }
70
71 static int is_enabled(struct optcfg *oc)
72 {
73         int res;
74         optcfg_enabled_value(oc, &res);
75         return res != 0;
76 }
77
78 static int opt_handler(struct optcfg *oc, int optid, void *cls)
79 {
80         char *valstr;
81
82         switch(optid) {
83         case OPTCFG_SIZE:
84                 valstr = optcfg_next_value(oc);
85                 if(!valstr || sscanf(valstr, "%dx%d", &opt.width, &opt.height) != 2) {
86                         fprintf(stderr, "size must be of the form: WIDTHxHEIGHT\n");
87                         return -1;
88                 }
89                 break;
90
91 #ifdef BUILD_VR
92         case OPTCFG_VR:
93                 if(is_enabled(oc)) {
94                         opt.flags |= OPT_VR;
95                 } else {
96                         opt.flags &= ~OPT_VR;
97                 }
98                 break;
99 #endif
100
101         case OPTCFG_FULLSCREEN:
102                 if(is_enabled(oc)) {
103                         opt.flags |= OPT_FULLSCREEN;
104                 } else {
105                         opt.flags &= ~OPT_FULLSCREEN;
106                 }
107                 break;
108
109         case OPTCFG_WINDOWED:
110                 if(is_enabled(oc)) {
111                         opt.flags &= ~OPT_FULLSCREEN;
112                 } else {
113                         opt.flags |= OPT_FULLSCREEN;
114                 }
115                 break;
116
117         case OPTCFG_SCREEN:
118                 if(!(valstr = optcfg_next_value(oc))) {
119                         fprintf(stderr, "screen name missing\n");
120                         return -1;
121                 }
122                 free(opt.start_scr);
123                 if(!(opt.start_scr = malloc(strlen(valstr) + 1))) {
124                         perror("failed to allocate memory");
125                         return -1;
126                 }
127                 strcpy(opt.start_scr, valstr);
128                 break;
129
130         case OPTCFG_HELP:
131                 printf("Usage: %s [options]\nOptions:\n", argv0);
132                 optcfg_print_options(oc);
133                 exit(0);
134         }
135         return 0;
136 }
137
138 static int arg_handler(struct optcfg *oc, const char *arg, void *cls)
139 {
140         fprintf(stderr, "unexpected argument: %s\n", arg);
141         return -1;
142 }