-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_cmdline.c
402 lines (385 loc) · 10.4 KB
/
parse_cmdline.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
gpioctl
Copyright (C) 2019 Jörn Nettingsmeier
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "parse_cmdline.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <limits.h>
#include "globals.h"
#include "build/config.h"
#include "stdout_cmdline.h"
#ifdef HAVE_JACK
#include "jack_cmdline.h"
#endif
#ifdef HAVE_ALSA
#include "alsa_cmdline.h"
#include "slave_cmdline.h"
#endif
#ifdef HAVE_OSC
#include "osc_cmdline.h"
#include "master_cmdline.h"
#endif
void usage()
{
printf("\n%s v%s handles switches and rotary encoders connected to GPIOs, using the\n", PROGRAM_NAME, PROGRAM_VERSION);
printf("portable libgpiod kernel interface, to send text messages to /dev/stdout.\n");
printf("If enabled at build time, you can also send JACK MIDI CC messages,\n");
printf("OSC messages, or directly interact with an ALSA mixer control.\n");
printf("We assume GPI pins have a pull-up, so the return should be connected to ground.\n\n");
printf("-h|--help This help.\n");
printf("-V|--version Print version and exit.\n");
printf("-v|--verbose Print current controller values.\n\n");
printf("The following options may be specified multiple times. All parameters must be\n");
printf("separated by commas, no spaces. Parameters in brackets are optional.\n\n");
printf("-r|--rotary clk,dt,type,...\n");
printf(" Set up a rotary encoder.\n");
printf(" clk: the GPI number of the first encoder contact (0-%d)\n", MAXGPIO - 1);
printf(" dt: the GPI number of the second encoder contact (0-%d)\n", MAXGPIO - 1);
printf(" Depending on 'type', the remaining parameters are:\n\n");
#ifdef HAVE_JACK
help_rotary_JACK();
printf("\n");
#endif
#ifdef HAVE_ALSA
help_rotary_ALSA();
printf("\n");
#endif
#ifdef HAVE_OSC
help_rotary_OSC();
printf("\n");
help_rotary_MASTER();
printf("\n");
#endif
help_rotary_STDOUT();
printf("\n");
printf("-s|--switch sw,type...\n");
printf(" Set up a switch.\n");
printf(" sw: the GPI pin number of the switch contact (0-%d)\n", MAXGPIO - 1);
printf(" Depending on 'type', the remaining parameters are:\n\n");
#ifdef HAVE_JACK
help_switch_JACK();
printf("\n");
#endif
#ifdef HAVE_ALSA
help_switch_ALSA();
printf("\n");
#endif
#ifdef HAVE_OSC
help_switch_OSC();
printf("\n");
help_switch_MASTER();
printf("\n");
#endif
help_switch_STDOUT();
printf("\n");
#ifdef HAVE_OSC
# ifdef HAVE_ALSA
help_SLAVE();
# endif
#endif
printf("Pin numbers above are hardware GPIO numbers. They do not usually correspond\n");
printf("to physical pin numbers. For the RPi, check https://pinout.xyz/# and look\n");
printf("for the Broadcom ('BCM') numbers.\n");
printf("libgpiod does not know how to control the pull-up/pull-down resistors of your\n");
printf("GPIO pins. Use a hardware-specific external tool to enable them, or add\n");
printf("physical pull-ups.\n\n");
printf("%s is meant to run as a daemon. Use CTRL-C or send a SIGTERM to exit.\n\n",
PROGRAM_NAME);
}
static void debugmsg(control_t * c)
{
DBG("Parsed control %s(%d|%d)->%s [%d..%d] step=%d toggle=%d midi_ch=%d midi_cc=%d param1=%s param2=%s value=%d",
control_types[c->type], c->pin1, c->pin2, control_targets[c->target], c->min, c->max, c->step, c->toggle, c->midi_ch, c->midi_cc,
(c->param1 == NULL) ? "''" : (char *)c->param1, (c->param2 == NULL) ? "''" : (char *)c->param2, c->value);
}
static int tokenize(char *argument, char *config[])
{
int i = 0;
if (argument == NULL) return 0;
config[0] = strtok(argument, ",");
// always handle MAXARG entries, to make sure we overwrite all previous hits with NULL
for (int k = 1; k < MAXARG; k++) {
if (config[i] != NULL)
i++;
config[k] = strtok(NULL, ",");
}
return i;
}
static int match(char *string1, char *string2)
{
if (strncmp(string1, string2, strlen(string2)) == 0) {
return 1;
} else {
return 0;
}
}
int parse_cmdline(int argc, char *argv[])
{
int o;
int i;
char *config[MAXARG];
int ncontrols = 0;
control_t *c;
control_t *d;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"verbose", no_argument, 0, 'v'},
{"rotary", required_argument, 0, 'r'},
{"switch", required_argument, 0, 's'},
{"slave-rotary", required_argument, 0, 'R'},
{"slave-switch", required_argument, 0, 'S'},
{0, 0, 0, 0}
};
while (1) {
int optind = 0;
c = NULL;
d = NULL;
o = getopt_long(argc, argv, ":hVvr:s:U:R:S:", long_options, &optind);
if (o == -1)
break;
i = tokenize(optarg, config);
switch (o) {
case 'h':
return EXIT_USAGE;
case 'V':
return EXIT_VERSION;
case 'v':
verbose = 1;
continue; // skip controls update at end
case 'r':
c = calloc(sizeof(control_t), 1);
d = calloc(sizeof(control_t), 1);
if (c == NULL || d == NULL) {
ERR("calloc() failed.");
goto error;
}
if (i < 3) {
ERR("Not enough options for -r.");
goto error;
}
c->pin1 = atoi(config[0]);
if (c->pin1 < 0 || c->pin1 >= MAXGPIO) {
ERR("clk value out of range.");
goto error;
}
if (controller[c->pin1] != NULL) {
ERR("clk pin already assigned.");
goto error;
}
c->pin2 = atoi(config[1]);
if (c->pin2 < 0 || c->pin2 >= MAXGPIO) {
ERR("dt value of of range.");
goto error;
}
if (controller[c->pin2] != NULL) {
ERR("dt pin already assigned.");
goto error;
}
controller[c->pin1] = c;
c->type = ROTARY;
c->param1 = calloc(sizeof(char), MAXNAME);
c->param2 = calloc(sizeof(char), MAXNAME);
if (c->param1 == NULL || c->param2 == NULL) {
ERR("calloc() failed.");
goto error;
}
#ifdef HAVE_JACK
if (match(config[2], "jack")) {
if (parse_cmdline_rotary_JACK(c, config))
goto error;
use_jack = 1;
} else
#endif
#ifdef HAVE_ALSA
if (match(config[2], "alsa")) {
if (parse_cmdline_rotary_ALSA(c, config))
goto error;
use_alsa = 1;
} else
#endif
#ifdef HAVE_OSC
if (match(config[2], "osc")) {
if (parse_cmdline_rotary_OSC(c, config))
goto error;
use_osc = 1;
} else
if (match(config[2], "master")) {
if (parse_cmdline_rotary_MASTER(c, config))
goto error;
use_osc = 1;
} else
#endif
if (match(config[2], "stdout")) {
if (parse_cmdline_rotary_STDOUT(c, config))
goto error;
use_stdout = 1;
} else {
ERR("Unknown type '%s'.", config[2]);
goto error;
}
controller[c->pin2] = d;
d->type = AUX;
d->target = c->target;
break;
case 's':
c = calloc(sizeof(control_t), 1);
if (c == NULL) {
ERR("calloc() failed.");
goto error;
}
if (i < 2) {
ERR("Not enough options for -s.");
goto error;
}
c->pin1 = atoi(config[0]);
if (c->pin1 < 0 || c->pin1 >= MAXGPIO) {
ERR("sw value out of range.");
goto error;
}
if (controller[c->pin1] != NULL) {
ERR("sw pin already assigned.");
goto error;
}
controller[c->pin1] = c;
c->type = SWITCH;
c->param1 = calloc(sizeof(char), MAXNAME);
c->param2 = calloc(sizeof(char), MAXNAME);
#ifdef HAVE_JACK
if (match(config[1], "jack")) {
if (parse_cmdline_switch_JACK(c, config))
goto error;
use_jack = 1;
} else
#endif
#ifdef HAVE_ALSA
if (match(config[1], "alsa")) {
if (parse_cmdline_switch_ALSA(c, config))
goto error;
use_alsa = 1;
} else
#endif
#ifdef HAVE_OSC
if (match(config[1], "osc")) {
if (parse_cmdline_switch_OSC(c, config))
goto error;
use_osc = 1;
} else
if (match(config[1], "master")) {
if (parse_cmdline_switch_MASTER(c, config))
goto error;
use_osc = 1;
} else
#endif
if (match(config[1], "stdout")) {
if (parse_cmdline_switch_STDOUT(c, config))
goto error;
use_stdout = 1;
} else {
ERR("Unknown type '%s'.", config[2]);
goto error;
}
break;
#ifdef HAVE_OSC
# ifdef HAVE_ALSA
case 'U':
osc_url = calloc(sizeof(char), MAXNAME);
if (osc_url == NULL) {
ERR("calloc() failed.");
goto error;
}
if (config[0] == NULL) {
ERR("osc-url must be set.");
goto error;
}
osc_url = strncpy(osc_url, config[0], MAXNAME);
continue; // skip controls update at end
case 'R':
c = calloc(sizeof(control_t), 1);
if (c == NULL) {
ERR("calloc() failed.");
goto error;
}
c->param1 = calloc(sizeof(char), MAXNAME);
c->param2 = calloc(sizeof(char), MAXNAME);
if (c->param1 == NULL || c->param2 == NULL) {
ERR("calloc() failed.");
goto error;
}
if (parse_cmdline_rotary_SLAVE(c, config))
goto error;
controller[c->pin1] = c;
use_slave = 1;
use_alsa = 1;
break;
case 'S':
c = calloc(sizeof(control_t), 1);
if (c == NULL) {
ERR("calloc() failed.");
goto error;
}
c->param1 = calloc(sizeof(char), MAXNAME);
c->param2 = calloc(sizeof(char), MAXNAME);
if (c->param1 == NULL || c->param2 == NULL) {
ERR("calloc() failed.");
goto error;
}
if (parse_cmdline_switch_SLAVE(c, config))
goto error;
controller[c->pin1] = c;
use_slave = 1;
use_alsa = 1;
break;
# endif
#endif
case ':':
ERR("Option '-%c' requires an argument.", optopt);
goto error;
default:
ERR("Unknown option -%c.", optopt);
goto error;
}
ncontrols++;
debugmsg(c);
}
if (ncontrols < 1) {
ERR("You need to set at least one control.");
goto error;
}
if (use_slave && (osc_url == NULL)) {
ERR("You need to specifiy -U with -R and -S.");
goto error;
}
return EXIT_CLEAN;
error:{
if (c != NULL) {
if (c->param1 != NULL)
free(c->param1);
if (c->param2 != NULL)
free(c->param2);
free(c);
}
if (d != NULL) {
if (d->param1 != NULL)
free(d->param1);
if (d->param2 != NULL)
free(d->param2);
free(d);
}
printf("Use -h for help.\n");
return EXIT_ERR;
}
}