-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainopt.c
277 lines (245 loc) · 7.82 KB
/
mainopt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Process applications command line options
*/
// System <includes>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
// Application "includes"
#include "main.h"
#include "mainopt.h"
//*******************************************************
// Defines
//*******************************************************
// For conversions where no conversion type is given
#define EOL_DEFAULT "\n"
// Valid short options
#define VALIDOPTS "Vhe:p:x:y:wa:f:s:"
// Short ID's for long options, avoid conflict with above characters
#define OPT_VERSION 'V'
#define OPT_HELP 'h'
#define OPT_FULL_SCREEN 'w'
#define OPT_ELEMENTS 'e'
#define OPT_ENV_IN_PHASE 'p'
#define OPT_F_MAX 'f'
#define OPT_BASE_RES 'r'
#define OPT_XRES 'x'
#define OPT_YRES 'y'
#define OPT_SPEED 's'
#define OPT_AMPLITUDE 'a'
#define MSG_USAGE "\n\
Usage: "MAIN_TITLE" [OPTIONS] \n\
\n\
\n\
-w, --fullscreen run in fullscreen\n\
-e, --elements=N number of elements (1 -> N) default 5\n\
-p, --inphase=N number of envelopes in phase (1 -> number of elements) default 2\n\
-f, --fmax=N highest frequency (1 -> N) default 8\n\
-r, --loopres=N loop resolution (256 -> N) default 1024 *must be power of 2*\n\
-x, --xres=N X window size, default 1000\n\
-y, --yres=N Y window size, default 1000\n\
-s, --speed=N Envelope speed, default 0.15\n\
-a, --amplitude=N Amplitude adjustment, default 1.0\n\
\n\
\n\
-h, --help display this help\n\
-V, --version display version information\n\
\n\
Examples:\n\
"MAIN_TITLE" --xres=1920 --yres=1080 --fullscreen\n\
"MAIN_TITLE" --loopres=4096 --elements=12 --inphase=1 --amplitude=2.0\n\
\n\
"
#define MSG_VERSION "\n\
"MAIN_TITLE" "MAIN_VERSION" \n\
Copyright (C) 2022 Michael Clift\n\
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
This is free software, and you are welcome to redistribute it.\n\
This program comes with ABSOLUTELY NO WARRANTY.\n\
\n\
"
//*******************************************************
// Variables
//*******************************************************
static struct mainopt_struct opt_defaults =
{
.full_screen = false,
.number_of_elements = 5,
.envelopes_in_phase = 2,
.element_freq_max = 8,
.base_resolution = 1024,
.window_width = 1000,
.window_height = 1000,
.envelope_speed = 0.15,
.amplitude = 1.0,
.finished = false,
};
static struct option long_options[] = {
{"fullscreen", no_argument, NULL, OPT_FULL_SCREEN},
{"elements", required_argument, NULL, OPT_ELEMENTS},
{"inphase", required_argument, NULL, OPT_ENV_IN_PHASE},
{"fmax", required_argument, NULL, OPT_F_MAX},
{"loopres", required_argument, NULL, OPT_BASE_RES},
{"xres", required_argument, NULL, OPT_XRES},
{"yres", required_argument, NULL, OPT_YRES},
{"speed" , required_argument, NULL, OPT_SPEED},
{"amplitude", required_argument, NULL, OPT_AMPLITUDE},
{"help" , no_argument, NULL, OPT_HELP},
{"version" , no_argument, NULL, OPT_VERSION},
{0,0,0,0},
};
//*******************************************************
// Prototypes
//*******************************************************
// Process a single option
static void process_option(struct mainopt_struct *retval, int x, const char* argument);
// Check options are valid
static void validate_options(struct mainopt_struct *retval);
static bool isPowerOfTwo(int x);
//*******************************************************
// Functions
//*******************************************************
struct mainopt_struct mainopt_parse(int argc, char *argv[])
{
int x = 0;
bool finished = false;
struct mainopt_struct retval = opt_defaults;
do
{
x = getopt_long(argc, argv, VALIDOPTS, long_options, NULL);
if(x != -1)
{
process_option(&retval, x, optarg);
if(retval.finished) //(program may finish after displaying help or version)
finished = true;
}
else
finished = true;
}while(!finished);
if(!retval.finished)
validate_options(&retval);
return retval;
}
//*******************************************************
// Private Functions
//*******************************************************
// process a single option
static void process_option(struct mainopt_struct *retval, int x, const char* argument)
{
// Invalid option
if(x == '?')
{
retval->finished = true;
retval->error = true;
}
// Show help message
if(x == OPT_HELP)
{
printf(MSG_USAGE);
retval->finished = true;
}
// Show version message
else if(x == OPT_VERSION)
{
printf(MSG_VERSION);
retval->finished = true;
}
// Full screen
else if(x == OPT_FULL_SCREEN)
{
retval->full_screen = true;
}
else if(x == OPT_ELEMENTS)
{
if(!argument)
retval->finished = true;
else
retval->number_of_elements = atoi(argument);
}
else if(x == OPT_ENV_IN_PHASE)
{
if(!argument)
retval->finished = true;
else
retval->envelopes_in_phase = atoi(argument);
}
else if(x == OPT_F_MAX)
{
if(!argument)
retval->finished = true;
else
retval->element_freq_max = atoi(argument);
}
else if(x == OPT_BASE_RES)
{
if(!argument)
retval->finished = true;
else
retval->base_resolution = atoi(argument);
}
else if(x == OPT_XRES)
{
if(!argument)
retval->finished = true;
else
retval->window_width = atoi(argument);
}
else if(x == OPT_YRES)
{
if(!argument)
retval->finished = true;
else
retval->window_height = atoi(argument);
}
else if(x == OPT_SPEED)
{
if(!argument)
retval->finished = true;
else
retval->envelope_speed = atof(argument);
}
else if(x == OPT_AMPLITUDE)
{
if(!argument)
retval->finished = true;
else
retval->amplitude = atof(argument);
};
}
static void validate_options(struct mainopt_struct *retval)
{
if(retval->number_of_elements == 0)
{
fprintf(stderr, "*** ERROR Number of elements must not be 0\n");
retval->finished = true;
retval->error = true;
};
if(retval->envelopes_in_phase > retval->number_of_elements)
{
fprintf(stderr, "*** ERROR Envelopes in phase must not exceed number of elements (%i)\n", retval->number_of_elements);
retval->finished = true;
retval->error = true;
};
if(retval->envelopes_in_phase == 0)
retval->envelopes_in_phase = 1;
if(retval->element_freq_max == 0)
retval->element_freq_max = 1;
if(!isPowerOfTwo(retval->base_resolution))
{
fprintf(stderr, "*** ERROR Loop resolution must be a power of 2 ie. 512, 1024, 2048, 4096, 8192...\n");
retval->finished = true;
retval->error = true;
};
if(retval->window_width < 10)
retval->window_width = 10;
if(retval->window_height < 10)
retval->window_height = 10;
}
static bool isPowerOfTwo(int x)
{
return x && (!(x & (x - 1)));
}