Update copyright year list
[a2jmidid.git] / port.c
blobd9cb2ae8362383753aa3695f986090fdbe2a9db0
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * ALSA SEQ < - > JACK MIDI bridge
5 * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
6 * Copyright (c) 2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdbool.h>
23 #include <ctype.h>
24 #include <semaphore.h>
25 #include <alsa/asoundlib.h>
26 #include <jack/jack.h>
27 #include <jack/ringbuffer.h>
29 #include "list.h"
30 #include "structs.h"
31 #include "port_hash.h"
32 #include "log.h"
33 #include "port.h"
35 /* This should be part of JACK API */
36 #define JACK_IS_VALID_PORT_NAME_CHAR(c) \
37 (isalnum(c) || \
38 (c) == '/' || \
39 (c) == '_' || \
40 (c) == ':' || \
41 (c) == '(' || \
42 (c) == ')' || \
43 (c) == '-' || \
44 (c) == '[' || \
45 (c) == ']')
47 static
48 int
49 a2j_alsa_connect_from(
50 struct a2j * self,
51 int client,
52 int port)
54 snd_seq_port_subscribe_t* sub;
55 snd_seq_addr_t seq_addr;
56 int err;
58 snd_seq_port_subscribe_alloca(&sub);
59 seq_addr.client = client;
60 seq_addr.port = port;
61 snd_seq_port_subscribe_set_sender(sub, &seq_addr);
62 seq_addr.client = self->client_id;
63 seq_addr.port = self->port_id;
64 snd_seq_port_subscribe_set_dest(sub, &seq_addr);
66 snd_seq_port_subscribe_set_time_update(sub, 1);
67 snd_seq_port_subscribe_set_queue(sub, self->queue);
68 snd_seq_port_subscribe_set_time_real(sub, 1);
70 if ((err=snd_seq_subscribe_port(self->seq, sub)))
71 a2j_error("can't subscribe to %d:%d - %s", client, port, snd_strerror(err));
72 return err;
75 void
76 a2j_port_setdead(
77 a2j_port_hash_t hash,
78 snd_seq_addr_t addr)
80 struct a2j_port *port = a2j_port_get(hash, addr);
81 if (port)
82 port->is_dead = true; // see jack_process_internal
83 else
84 a2j_debug("port_setdead: not found (%d:%d)", addr.client, addr.port);
87 void
88 a2j_port_free(
89 struct a2j_port * port)
91 //snd_seq_disconnect_from(self->seq, self->port_id, port->remote.client, port->remote.port);
92 //snd_seq_disconnect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
93 if (port->inbound_events)
94 jack_ringbuffer_free(port->inbound_events);
95 if (port->jack_port != JACK_INVALID_PORT)
96 jack_port_unregister(port->a2j_ptr->jack_client, port->jack_port);
98 free(port);
101 void
102 a2j_port_fill_name(
103 struct a2j_port * port_ptr,
104 int type,
105 snd_seq_client_info_t * client_info_ptr,
106 const snd_seq_port_info_t * port_info_ptr,
107 bool make_unique)
109 char *c;
110 int ret;
112 if (make_unique)
114 ret = snprintf(
115 port_ptr->name,
116 g_max_jack_port_name_size,
117 "%s [%d] (%s): %s",
118 snd_seq_client_info_get_name(client_info_ptr),
119 snd_seq_client_info_get_client(client_info_ptr),
120 type == A2J_PORT_CAPTURE ? "capture": "playback",
121 snd_seq_port_info_get_name(port_info_ptr));
123 else
125 ret = snprintf(
126 port_ptr->name,
127 g_max_jack_port_name_size,
128 "%s (%s): %s",
129 snd_seq_client_info_get_name(client_info_ptr),
130 type == A2J_PORT_CAPTURE ? "capture": "playback",
131 snd_seq_port_info_get_name(port_info_ptr));
134 // replace all offending characters with ' '
135 for (c = port_ptr->name; *c; ++c)
136 if (!JACK_IS_VALID_PORT_NAME_CHAR(*c))
137 *c = ' ';
139 if (ret < 0 || ret >= g_max_jack_port_name_size)
141 /* force terminating nul char because the man page does not specify whether the resulting buffer is nul terminated in this case */
142 port_ptr->name[g_max_jack_port_name_size] = 0;
143 a2j_warning("port name \"%s\" was truncated because of the max size support by JACK (%zu)", port_ptr->name, g_max_jack_port_name_size);
147 struct a2j_port *
148 a2j_port_create(
149 struct a2j * self,
150 int type,
151 snd_seq_addr_t addr,
152 const snd_seq_port_info_t * info)
154 struct a2j_port *port;
155 int err;
156 int client;
157 snd_seq_client_info_t * client_info_ptr;
158 int jack_caps;
159 struct a2j_stream * stream_ptr;
161 stream_ptr = &self->stream[type];
163 err = snd_seq_client_info_malloc(&client_info_ptr);
164 if (err != 0)
166 a2j_error("Failed to allocate client info");
167 goto fail;
170 client = snd_seq_port_info_get_client(info);
172 err = snd_seq_get_any_client_info(self->seq, client, client_info_ptr);
173 if (err != 0)
175 a2j_error("Failed to get client info");
176 goto fail_free_client_info;
179 a2j_debug("client name: '%s'", snd_seq_client_info_get_name(client_info_ptr));
180 a2j_debug("port name: '%s'", snd_seq_port_info_get_name(info));
182 port = calloc(1, sizeof(struct a2j_port) + g_max_jack_port_name_size);
183 if (!port)
185 goto fail_free_client_info;
188 port->a2j_ptr = self;
190 port->jack_port = JACK_INVALID_PORT;
191 port->remote = addr;
193 a2j_port_fill_name(port, type, client_info_ptr, info, true);
195 /* Add port to list early, before registering to JACK, so map functionality is guaranteed to work during port registration */
196 list_add_tail(&port->siblings, &stream_ptr->list);
198 if (type == A2J_PORT_CAPTURE)
200 jack_caps = JackPortIsOutput;
202 else
204 jack_caps = JackPortIsInput;
207 /* mark anything that looks like a hardware port as physical&terminal */
208 if (snd_seq_port_info_get_type(info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC))
210 jack_caps |= JackPortIsPhysical|JackPortIsTerminal;
213 port->jack_port = jack_port_register(self->jack_client, port->name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
214 if (port->jack_port == JACK_INVALID_PORT)
216 a2j_error("jack_port_register() failed for '%s'", port->name);
217 goto fail_free_port;
220 if (type == A2J_PORT_CAPTURE)
221 err = a2j_alsa_connect_from(self, port->remote.client, port->remote.port);
222 else
223 err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
224 if (err)
226 a2j_info("port skipped: %s", port->name);
227 goto fail_free_port;
230 port->inbound_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
232 a2j_info("port created: %s", port->name);
233 return port;
235 fail_free_port:
236 list_del(&port->siblings);
238 a2j_port_free(port);
240 fail_free_client_info:
241 snd_seq_client_info_free(client_info_ptr);
243 fail:
244 return NULL;