-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
319 lines (297 loc) · 6.44 KB
/
main.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
/*
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 <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include "globals.h"
#include "parse_cmdline.h"
#include "gpiod_process.h"
#include "build/config.h"
#include "stdout_process.h"
#ifdef HAVE_JACK
#include "ringbuffer.h"
#include "jack_process.h"
#endif
#ifdef HAVE_ALSA
#include "alsa_process.h"
#endif
#ifdef HAVE_OSC
#include "osc_process.h"
#include "slave_process.h"
#endif
control_t *controller[NCONTROLLERS] = { 0 };
int verbose = 0;
int use_alsa = 0;
int use_jack = 0;
int use_osc = 0;
int use_stdout = 0;
int use_slave = 0;
char* osc_url;
const char* control_types[] = {
"NOCTL",
"AUX",
"ROTARY",
"SWITCH"
};
const char* control_targets[] = {
"NOTGT",
"ALSA",
"JACK",
"OSC",
"STDOUT",
"MASTER",
"SLAVE"
};
static void shutdown(int sig)
{
NFO("Received signal, terminating.");
#ifdef HAVE_ALSA
if (use_alsa) {
shutdown_ALSA();
}
#endif
#ifdef HAVE_JACK
if (use_jack) {
shutdown_JACK();
shutdown_ringbuffer();
}
#endif
#ifdef HAVE_OSC
if (use_osc) {
shutdown_OSC();
}
if (use_slave) {
shutdown_SLAVE();
}
#endif
shutdown_GPIOD();
exit(0);
}
void update(control_t* c, int delta)
{
DBG("update: delta = %d", delta);
switch (c->type) {
case ROTARY:
case AUX:
if (c->target == MASTER) {
// only send relative changes to slaves
c->value = delta * c->step;
break;
}
#ifdef HAVE_ALSA
if (c->target == ALSA || c->target == SLAVE) {
// to avoid loudness jumps, we always re-read the current mixer value
// in case it got changed by someone else, and then apply a relative
// change
c->value = get_ALSA_value(c);
// clamp to our range. some mixers have min values of -999999 and max
// values of +4 or so...
if (c->value < c->min) c->value = c->min;
if (c->value > c->max) c->value = c->max;
// fader taper:
if (c->value > -13) c->step = 1;
else if (c->value > -30) c->step = 2;
else if (c->value > -41) c->step = 3;
else if (c->value > -49) c->step = 4;
else if (c->value > -59) c->step = 5;
else if (c->value > -68) c->step = 9;
else if (c->value > -70) c->step = 10;
else c->step = 20;
}
#endif
if ((delta < 0 && c->value > c->min)) {
if (c->value - c->step > c->min) {
c->value -= c->step;
} else {
c->value = c->min;
}
} else if ((delta > 0 && c->value < c->max)) {
if (c->value + c->step < c->max) {
c->value += c->step;
} else {
c->value = c->max;
}
} else
return;
break;
case SWITCH:
if (c->toggle) {
if (delta == 0)
return;
if (c->value > c->min) {
c->value = c->min;
} else {
c->value = c->max;
}
} else {
if (delta == 0) {
c->value = c->min;
} else {
c->value = c->max;
}
}
break;
default:
ERR("Unknown c->type %d. THIS SHOULD NEVER HAPPEN.", c->type);
break;
}
switch (c->target) {
case STDOUT:
update_STDOUT(c);
break;
#ifdef HAVE_JACK
case JACK:
update_JACK(c);
break;
#endif
#ifdef HAVE_ALSA
case ALSA:
update_ALSA(c);
break;
case SLAVE:
update_ALSA(c);
break;
#endif
#ifdef HAVE_OSC
case OSC:
update_OSC(c);
break;
case MASTER:
update_OSC(c);
break;
#endif
default:
ERR("Unknown c->target %d. THIS SHOULD NEVER HAPPEN.",
c->target);
}
NFO("%s% 3d\t-> %s\t% 3d", control_types[c->type], c->pin1, control_targets[c->target], c->value);
}
void handle_gpi(int line, int delta)
{
// in order to properly debounce both rotary contacts,
// aux lines now get their own event handler on the libgpiod side.
// over here, we must redirect them to their rotary main line.
// the linear search is ugly. FIXME!
control_t *c = controller[line];
if (c->type == AUX) {
for (int i=0; i < NCONTROLLERS; i++) {
if (controller[i] == NULL) continue;
if (controller[i]->pin2 == line) {
update(controller[i], delta);
break;
}
}
} else {
update(c, delta);
}
}
void handle_osc(control_t *c, int delta) {
update(c, delta);
}
int main(int argc, char *argv[])
{
control_t *c;
int rval = parse_cmdline(argc, argv);
switch (rval) {
case EXIT_USAGE:
usage();
exit(0);
case EXIT_VERSION:
printf("%s %s\n", PROGRAM_NAME, PROGRAM_VERSION);
exit(0);
case EXIT_CLEAN:
break;
default:
exit(rval);
}
setup_GPIOD(GPIOD_DEVICE, PROGRAM_NAME, &handle_gpi);
#ifdef HAVE_JACK
if (use_jack) {
setup_ringbuffer(JACK_BUFSIZE);
setup_JACK();
}
#endif
#ifdef HAVE_ALSA
if (use_alsa) {
setup_ALSA();
}
#endif
#ifdef HAVE_OSC
if (use_osc) {
setup_OSC();
}
if (use_slave) {
if (setup_SLAVE(osc_url, &handle_osc))
exit(2); // fatal with segfaults down the line
}
#endif
for (int i = 0; i < NCONTROLLERS; i++) {
if (controller[i] == NULL)
continue;
c = controller[i];
switch (c->target) {
/* // can't happen since we gave auxes their own gpio handler
case NOTGT:
// this line is a dt pin for a rotary, without its own handler
continue;
*/
#ifdef HAVE_ALSA
case ALSA:
if (c->type == AUX) break;
c->param1 = setup_ALSA_elem(c->param1);
// fall-through
#endif
case JACK:
case OSC:
case STDOUT:
case MASTER:
switch (c->type) {
case ROTARY:
setup_GPIOD_rotary(c->pin1, c->pin2);
break;
case SWITCH:
setup_GPIOD_switch(c->pin1);
break;
case AUX:
// do nothing
break;
default:
ERR("c->type %d can't happen here. BUG?", c->type);
}
break;
#ifdef HAVE_ALSA
# ifdef HAVE_OSC
case SLAVE:
c->param1 = setup_ALSA_elem(c->param1);
setup_SLAVE_handler(c->param2, c);
break;
# endif
#endif
default:
ERR("Unknown c->target %d. THIS SHOULD NEVER HAPPEN.",
c->target);
}
}
signal(SIGTERM, &shutdown);
signal(SIGINT, &shutdown);
#ifdef HAVE_OSC
if (use_slave) start_SLAVE(); // this one spawns a thread
#endif
start_GPIOD(); // this one goes to sleep
sleep(-1);
}