git aware version script
[a2jmidid.git] / port.c
blob6d5b7deda56da0a81d14352c35203feed15e945e
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 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->early_events)
94 jack_ringbuffer_free(port->early_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;
111 if (make_unique)
113 snprintf(
114 port_ptr->name,
115 sizeof(port_ptr->name),
116 "%s [%d] (%s): %s",
117 snd_seq_client_info_get_name(client_info_ptr),
118 snd_seq_client_info_get_client(client_info_ptr),
119 type == A2J_PORT_CAPTURE ? "capture": "playback",
120 snd_seq_port_info_get_name(port_info_ptr));
122 else
124 snprintf(
125 port_ptr->name,
126 sizeof(port_ptr->name),
127 "%s (%s): %s",
128 snd_seq_client_info_get_name(client_info_ptr),
129 type == A2J_PORT_CAPTURE ? "capture": "playback",
130 snd_seq_port_info_get_name(port_info_ptr));
133 // replace all offending characters with ' '
134 for (c = port_ptr->name; *c; ++c)
135 if (!JACK_IS_VALID_PORT_NAME_CHAR(*c))
136 *c = ' ';
139 struct a2j_port *
140 a2j_port_create(
141 struct a2j * self,
142 int type,
143 snd_seq_addr_t addr,
144 const snd_seq_port_info_t * info)
146 struct a2j_port *port;
147 int err;
148 int client;
149 snd_seq_client_info_t * client_info_ptr;
150 int jack_caps;
151 struct a2j_stream * stream_ptr;
153 stream_ptr = &self->stream[type];
155 err = snd_seq_client_info_malloc(&client_info_ptr);
156 if (err != 0)
158 a2j_error("Failed to allocate client info");
159 goto fail;
162 client = snd_seq_port_info_get_client(info);
164 err = snd_seq_get_any_client_info(self->seq, client, client_info_ptr);
165 if (err != 0)
167 a2j_error("Failed to get client info");
168 goto fail_free_client_info;
171 a2j_debug("client name: '%s'", snd_seq_client_info_get_name(client_info_ptr));
172 a2j_debug("port name: '%s'", snd_seq_port_info_get_name(info));
174 port = calloc(1, sizeof(struct a2j_port));
175 if (!port)
177 goto fail_free_client_info;
180 port->a2j_ptr = self;
182 port->jack_port = JACK_INVALID_PORT;
183 port->remote = addr;
185 a2j_port_fill_name(port, type, client_info_ptr, info, true);
187 /* Add port to list early, before registering to JACK, so map functionality is guaranteed to work during port registration */
188 list_add_tail(&port->siblings, &stream_ptr->list);
190 if (type == A2J_PORT_CAPTURE)
192 jack_caps = JackPortIsOutput;
194 else
196 jack_caps = JackPortIsInput;
199 /* mark anything that looks like a hardware port as physical&terminal */
200 if (snd_seq_port_info_get_type(info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC))
202 jack_caps |= JackPortIsPhysical|JackPortIsTerminal;
205 port->jack_port = jack_port_register(self->jack_client, port->name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
206 if (port->jack_port == JACK_INVALID_PORT)
208 a2j_error("jack_port_register() failed for '%s'", port->name);
209 goto fail_free_port;
212 if (type == A2J_PORT_CAPTURE)
213 err = a2j_alsa_connect_from(self, port->remote.client, port->remote.port);
214 else
215 err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
216 if (err)
218 a2j_info("port skipped: %s", port->name);
219 goto fail_free_port;
222 port->early_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
224 a2j_info("port created: %s", port->name);
225 return port;
227 fail_free_port:
228 list_del(&port->siblings);
230 a2j_port_free(port);
232 fail_free_client_info:
233 snd_seq_client_info_free(client_info_ptr);
235 fail:
236 return NULL;