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