forked from michaelforney/oscmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
208 lines (190 loc) · 3.92 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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include "oscmix.h"
#include "arg.h"
#include "socket.h"
#include "util.h"
extern int dflag;
static int lflag;
static int iflag;
static int rfd, wfd;
static void
usage(void)
{
fprintf(stderr, "usage: oscmix [-dlm] [-r addr] [-s addr]\n");
exit(1);
}
static void *
midiread(void *arg)
{
unsigned char data[8192], *datapos, *dataend, *nextpos;
uint_least32_t payload[sizeof data / 4];
ssize_t ret;
dataend = data;
for (;;) {
ret = read(6, dataend, (data + sizeof data) - dataend);
if (ret < 0)
fatal("read 6:");
dataend += ret;
datapos = data;
for (;;) {
assert(datapos <= dataend);
datapos = memchr(datapos, 0xf0, dataend - datapos);
if (!datapos) {
dataend = data;
break;
}
nextpos = memchr(datapos + 1, 0xf7, dataend - datapos - 1);
if (!nextpos) {
if (dataend == data + sizeof data) {
fprintf(stderr, "sysex packet too large; dropping\n");
dataend = data;
} else {
memmove(data, datapos, dataend - datapos);
dataend -= datapos - data;
}
break;
}
++nextpos;
handlesysex(datapos, nextpos - datapos, payload);
datapos = nextpos;
}
}
return NULL;
}
static void *
oscread(void *arg)
{
int fd;
ssize_t ret;
unsigned char buf[8192];
fd = *(int *)arg;
for (;;) {
ret = read(fd, buf, sizeof buf);
if (ret < 0) {
perror("recv");
break;
}
handleosc(buf, ret);
}
return NULL;
}
void
writemidi(const void *buf, size_t len)
{
const unsigned char *pos;
ssize_t ret;
pos = buf;
while (len > 0) {
ret = write(7, pos, len);
if (ret < 0)
fatal("write 7:");
pos += ret;
len -= ret;
}
}
void
writeosc(const void *buf, size_t len)
{
ssize_t ret;
ret = write(wfd, buf, len);
if (ret < 0) {
if (errno != ECONNREFUSED)
perror("write");
} else if (ret != len) {
fprintf(stderr, "write: %zd != %zu", ret, len);
}
}
int
main(int argc, char *argv[])
{
static char defrecvaddr[] = "udp!127.0.0.1!7222";
static char defsendaddr[] = "udp!127.0.0.1!8222";
static char mcastaddr[] = "udp!224.0.0.1!8222";
static const unsigned char refreshosc[] = "/refresh\0\0\0\0,\0\0\0";
int err, sig;
char *recvaddr, *sendaddr;
pthread_t midireader, oscreader;
struct itimerval it;
sigset_t set;
const char *port;
if (fcntl(6, F_GETFD) < 0)
fatal("fcntl 6:");
if (fcntl(7, F_GETFD) < 0)
fatal("fcntl 7:");
recvaddr = defrecvaddr;
sendaddr = defsendaddr;
port = NULL;
ARGBEGIN {
case 'd':
dflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'r':
recvaddr = EARGF(usage());
break;
case 's':
sendaddr = EARGF(usage());
break;
case 'm':
sendaddr = mcastaddr;
break;
case 'p':
port = EARGF(usage());
break;
default:
usage();
break;
} ARGEND
rfd = sockopen(recvaddr, 1);
wfd = sockopen(sendaddr, 0);
if (!port) {
port = getenv("MIDIPORT");
if (!port)
fatal("device is not specified; pass -p or set MIDIPORT");
}
if (init(port) != 0)
return 1;
sigfillset(&set);
pthread_sigmask(SIG_SETMASK, &set, NULL);
err = pthread_create(&midireader, NULL, midiread, &wfd);
if (err)
fatal("pthread_create: %s", strerror(err));
err = pthread_create(&oscreader, NULL, oscread, &rfd);
if (err)
fatal("pthread_create: %s", strerror(err));
sigemptyset(&set);
sigaddset(&set, SIGALRM);
pthread_sigmask(SIG_SETMASK, &set, NULL);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 100000;
it.it_value = it.it_interval;
if (setitimer(ITIMER_REAL, &it, NULL) != 0)
fatal("setitimer:");
printf("Before handleosc call\n");
handleosc(refreshosc, sizeof refreshosc - 1);
printf("After handleosc call\n");
for (;;) {
sigwait(&set, &sig);
if (lflag == 0) {
handletimer(true, false);
}
//else handletimer(true, true);
}
}