forked from jackaudio/a2jmidid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_thread.c
275 lines (236 loc) · 6.06 KB
/
port_thread.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
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* ALSA SEQ < - > JACK MIDI bridge
*
* Copyright (c) 2006,2007 Dmitry S. Baikov <[email protected]>
* Copyright (c) 2007,2008,2009 Nedko Arnaudov <[email protected]>
*
* 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; version 2 of the License.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdbool.h>
#include <semaphore.h>
#include <alsa/asoundlib.h>
#include <jack/jack.h>
#include <jack/ringbuffer.h>
#include "list.h"
#include "structs.h"
#include "port.h"
#include "port_hash.h"
#include "log.h"
#include "port_thread.h"
#include "conf.h"
struct a2j_port *
a2j_find_port_by_addr(
struct a2j_stream * stream_ptr,
snd_seq_addr_t addr)
{
struct list_head * node_ptr;
struct a2j_port * port_ptr;
list_for_each(node_ptr, &stream_ptr->list)
{
port_ptr = list_entry(node_ptr, struct a2j_port, siblings);
if (port_ptr->remote.client == addr.client && port_ptr->remote.port == addr.port)
{
return port_ptr;
}
}
return NULL;
}
struct a2j_port *
a2j_find_port_by_jack_port_name(
struct a2j_stream * stream_ptr,
const char * jack_port)
{
struct list_head * node_ptr;
struct a2j_port * port_ptr;
list_for_each(node_ptr, &stream_ptr->list)
{
port_ptr = list_entry(node_ptr, struct a2j_port, siblings);
if (strcmp(port_ptr->name, jack_port) == 0)
{
return port_ptr;
}
}
return NULL;
}
/*
* ==================== Port add/del handling thread ==============================
*/
static
void
a2j_update_port_type(
struct a2j * self,
int type,
snd_seq_addr_t addr,
int caps,
const snd_seq_port_info_t * info)
{
struct a2j_stream * stream_ptr;
int alsa_mask;
struct a2j_port * port_ptr;
a2j_debug("update_port_type(%d:%d)", addr.client, addr.port);
stream_ptr = &self->stream[type];
port_ptr = a2j_find_port_by_addr(stream_ptr, addr);
if (type == A2J_PORT_CAPTURE)
{
alsa_mask = SND_SEQ_PORT_CAP_SUBS_READ;
}
else
{
alsa_mask = SND_SEQ_PORT_CAP_SUBS_WRITE;
}
if (port_ptr != NULL && (caps & alsa_mask) != alsa_mask)
{
a2j_debug("setdead: %s", port_ptr->name);
port_ptr->is_dead = true;
}
if (port_ptr == NULL && (caps & alsa_mask) == alsa_mask)
{
if(jack_ringbuffer_write_space(stream_ptr->new_ports) >= sizeof(port_ptr)) {
port_ptr = a2j_port_create(self, type, addr, info);
if (port_ptr != NULL)
{
jack_ringbuffer_write(stream_ptr->new_ports, (char *)&port_ptr, sizeof(port_ptr));
}
} else {
a2j_error( "dropping new port event... increase MAX_PORTS" );
}
}
}
void
a2j_update_port(
struct a2j * self,
snd_seq_addr_t addr,
const snd_seq_port_info_t * info)
{
unsigned int port_caps = snd_seq_port_info_get_capability(info);
unsigned int port_type = snd_seq_port_info_get_type(info);
a2j_debug("port %u:%u", addr.client, addr.port);
a2j_debug("port type: 0x%08X", port_type);
a2j_debug("port caps: 0x%08X", port_caps);
if (port_type & SND_SEQ_PORT_TYPE_SPECIFIC)
{
a2j_debug("SPECIFIC");
}
if (port_type & SND_SEQ_PORT_TYPE_MIDI_GENERIC)
{
a2j_debug("MIDI_GENERIC");
}
if (port_type & SND_SEQ_PORT_TYPE_MIDI_GM)
{
a2j_debug("MIDI_GM");
}
if (port_type & SND_SEQ_PORT_TYPE_MIDI_GS)
{
a2j_debug("MIDI_GS");
}
if (port_type & SND_SEQ_PORT_TYPE_MIDI_XG)
{
a2j_debug("MIDI_XG");
}
if (port_type & SND_SEQ_PORT_TYPE_MIDI_MT32)
{
a2j_debug("MIDI_MT32");
}
if (port_type & SND_SEQ_PORT_TYPE_MIDI_GM2)
{
a2j_debug("MIDI_GM2");
}
if (port_type & SND_SEQ_PORT_TYPE_SYNTH)
{
a2j_debug("SYNTH");
}
if (port_type & SND_SEQ_PORT_TYPE_DIRECT_SAMPLE)
{
a2j_debug("DIRECT_SAMPLE");
}
if (port_type & SND_SEQ_PORT_TYPE_SAMPLE)
{
a2j_debug("SAMPLE");
}
if (port_type & SND_SEQ_PORT_TYPE_HARDWARE)
{
a2j_debug("HARDWARE");
}
if (port_type & SND_SEQ_PORT_TYPE_SOFTWARE)
{
a2j_debug("SOFTWARE");
}
if (port_type & SND_SEQ_PORT_TYPE_SYNTHESIZER)
{
a2j_debug("SYNTHESIZER");
}
if (port_type & SND_SEQ_PORT_TYPE_PORT)
{
a2j_debug("PORT");
}
if (port_type & SND_SEQ_PORT_TYPE_APPLICATION)
{
a2j_debug("APPLICATION");
}
if (port_type == 0)
{
a2j_debug("Ignoring port of type 0");
return;
}
if ((port_type & SND_SEQ_PORT_TYPE_HARDWARE) && !g_a2j_export_hw_ports)
{
a2j_debug("Ignoring hardware port");
return;
}
if (port_caps & SND_SEQ_PORT_CAP_NO_EXPORT)
{
a2j_debug("Ignoring no-export port");
return;
}
a2j_update_port_type(self, A2J_PORT_CAPTURE, addr, port_caps, info);
a2j_update_port_type(self, A2J_PORT_PLAYBACK, addr, port_caps, info);
}
void
a2j_free_ports(
jack_ringbuffer_t * ports)
{
struct a2j_port *port;
int sz;
while ((sz = jack_ringbuffer_read(ports, (char*)&port, sizeof(port)))) {
assert (sz == sizeof(port));
a2j_info("port deleted: %s", port->name);
list_del(&port->siblings);
a2j_port_free(port);
}
}
void
a2j_update_ports(
struct a2j * self)
{
snd_seq_addr_t addr;
int size;
while ((size = jack_ringbuffer_read(self->port_add, (char *)&addr, sizeof(addr))) != 0)
{
snd_seq_port_info_t * info;
int err;
snd_seq_port_info_alloca(&info);
assert(size == sizeof(addr));
assert(addr.client != self->client_id);
if ((err = snd_seq_get_any_port_info(self->seq, addr.client, addr.port, info)) >= 0)
{
a2j_update_port(self, addr, info);
}
else
{
//a2j_port_setdead(self->stream[A2J_PORT_CAPTURE].ports, addr);
//a2j_port_setdead(self->stream[A2J_PORT_PLAYBACK].ports, addr);
}
}
}