]> git.sesse.net Git - nageru/blob - futatabi/flags.cpp
7a8246030abf7d485836d467800612aaef5a7c95
[nageru] / futatabi / flags.cpp
1 #include "flags.h"
2
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <utility>
9
10 using namespace std;
11
12 Flags global_flags;
13
14 // Long options that have no corresponding short option.
15 enum LongOption {
16         OPTION_HELP = 1000,
17         OPTION_SLOW_DOWN_INPUT = 1001,
18         OPTION_HTTP_PORT = 1002
19 };
20
21 void usage()
22 {
23         fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n");
24         fprintf(stderr, "\n");
25         fprintf(stderr, "      --help                      print usage information\n");
26         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
27         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
28         fprintf(stderr, "  -r, --frame-rate NUM[/NUM]      output frame rate, as a float or fraction\n");
29         fprintf(stderr, "                                    (default 60000/1001 ~= 59.94)\n");
30         fprintf(stderr, "      --slow-down-input           slow down input to realtime (default on if no\n");
31         fprintf(stderr, "                                    source URL given)\n");
32         fprintf(stderr, "  -q, --interpolation-quality N   0 = off\n");
33         fprintf(stderr, "                                  1 = fastest\n");
34         fprintf(stderr, "                                  2 = default (realtime 720p on fast embedded GPUs)\n");
35         fprintf(stderr, "                                  3 = good (realtime 720p on GTX 970 or so)\n");
36         fprintf(stderr, "                                  4 = best (not realtime on any current GPU)\n");
37         fprintf(stderr, "  -d, --working-directory DIR     where to store frames and database\n");
38         fprintf(stderr, "      --http-port PORT            which port to listen on for output\n");
39 }
40
41 void parse_flags(int argc, char * const argv[])
42 {
43         static const option long_options[] = {
44                 { "help", no_argument, 0, OPTION_HELP },
45                 { "width", required_argument, 0, 'w' },
46                 { "height", required_argument, 0, 'h' },
47                 { "frame-rate", required_argument, 0, 'r' },
48                 { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT },
49                 { "interpolation-quality", required_argument, 0, 'q' },
50                 { "working-directory", required_argument, 0, 'd' },
51                 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
52                 { 0, 0, 0, 0 }
53         };
54         for ( ;; ) {
55                 int option_index = 0;
56                 int c = getopt_long(argc, argv, "w:h:r:q:d:", long_options, &option_index);
57
58                 if (c == -1) {
59                         break;
60                 }
61                 switch (c) {
62                 case 'w':
63                         global_flags.width = atoi(optarg);
64                         break;
65                 case 'h':
66                         global_flags.height = atoi(optarg);
67                         break;
68                 case 'r': {
69                         double num, den;
70                         if (sscanf(optarg, "%lf/%lf", &num, &den) == 2) {
71                                 global_flags.output_framerate = num / den;
72                         } else if (sscanf(optarg, "%lf", &num) == 1) {
73                                 global_flags.output_framerate = num;
74                         } else {
75                                 fprintf(stderr, "Invalid frame rate given (must be on the form N or N/M)\n");
76                                 exit(1);
77                         }
78                         break;
79                 }
80                 case OPTION_SLOW_DOWN_INPUT:
81                         global_flags.slow_down_input = true;
82                         break;
83                 case 'q':
84                         global_flags.interpolation_quality = atoi(optarg);
85                         break;
86                 case 'd':
87                         global_flags.working_directory = optarg;
88                         break;
89                 case OPTION_HTTP_PORT:
90                         global_flags.http_port = atoi(optarg);
91                         break;
92                 case OPTION_HELP:
93                         usage();
94                         exit(0);
95                 default:
96                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
97                         fprintf(stderr, "\n");
98                         usage();
99                         exit(1);
100                 }
101         }
102
103         if (global_flags.interpolation_quality < 0 || global_flags.interpolation_quality > 4) {
104                 fprintf(stderr, "Interpolation quality must be 0, 1, 2, 3 or 4.\n");
105                 usage();
106                 exit(1);
107         }
108 }