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