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