add dbus command to allow checking of wether hardware is currently exported
[a2jmidid.git] / port.c
blob3a7c41ec2cb056488ca844721fa4ba29b1b10275
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,2011 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 extern bool g_disable_port_uniqueness;
37 /* This should be part of JACK API */
38 #define JACK_IS_VALID_PORT_NAME_CHAR(c) \
39 (isalnum(c) || \
40 (c) == '/' || \
41 (c) == '_' || \
42 (c) == ':' || \
43 (c) == '(' || \
44 (c) == ')' || \
45 (c) == '-' || \
46 (c) == '[' || \
47 (c) == ']')
49 static
50 int
51 a2j_alsa_connect_from(
52 struct a2j * self,
53 int client,
54 int port)
56 snd_seq_port_subscribe_t* sub;
57 snd_seq_addr_t seq_addr;
58 int err;
60 snd_seq_port_subscribe_alloca(&sub);
61 seq_addr.client = client;
62 seq_addr.port = port;
63 snd_seq_port_subscribe_set_sender(sub, &seq_addr);
64 seq_addr.client = self->client_id;
65 seq_addr.port = self->port_id;
66 snd_seq_port_subscribe_set_dest(sub, &seq_addr);
68 snd_seq_port_subscribe_set_time_update(sub, 1);
69 snd_seq_port_subscribe_set_queue(sub, self->queue);
70 snd_seq_port_subscribe_set_time_real(sub, 1);
72 if ((err=snd_seq_subscribe_port(self->seq, sub)))
73 a2j_error("can't subscribe to %d:%d - %s", client, port, snd_strerror(err));
74 return err;
77 void
78 a2j_port_setdead(
79 a2j_port_hash_t hash,
80 snd_seq_addr_t addr)
82 struct a2j_port *port = a2j_port_get(hash, addr);
83 if (port)
84 port->is_dead = true; // see jack_process_internal
85 else
86 a2j_debug("port_setdead: not found (%d:%d)", addr.client, addr.port);
89 void
90 a2j_port_free(
91 struct a2j_port * port)
93 //snd_seq_disconnect_from(self->seq, self->port_id, port->remote.client, port->remote.port);
94 //snd_seq_disconnect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
95 if (port->inbound_events)
96 jack_ringbuffer_free(port->inbound_events);
97 if (port->jack_port != JACK_INVALID_PORT)
98 jack_port_unregister(port->a2j_ptr->jack_client, port->jack_port);
100 free(port);
103 void
104 a2j_port_fill_name(
105 struct a2j_port * port_ptr,
106 int type,
107 snd_seq_client_info_t * client_info_ptr,
108 const snd_seq_port_info_t * port_info_ptr,
109 bool make_unique)
111 char *c;
112 int ret;
114 if (make_unique)
116 ret = snprintf(
117 port_ptr->name,
118 g_max_jack_port_name_size,
119 "%s [%d] (%s): %s",
120 snd_seq_client_info_get_name(client_info_ptr),
121 snd_seq_client_info_get_client(client_info_ptr),
122 type == A2J_PORT_CAPTURE ? "capture": "playback",
123 snd_seq_port_info_get_name(port_info_ptr));
125 else
127 ret = snprintf(
128 port_ptr->name,
129 g_max_jack_port_name_size,
130 "%s (%s): %s",
131 snd_seq_client_info_get_name(client_info_ptr),
132 type == A2J_PORT_CAPTURE ? "capture": "playback",
133 snd_seq_port_info_get_name(port_info_ptr));
136 // replace all offending characters with ' '
137 for (c = port_ptr->name; *c; ++c)
138 if (!JACK_IS_VALID_PORT_NAME_CHAR(*c))
139 *c = ' ';
141 if (ret < 0 || ret >= g_max_jack_port_name_size)
143 /* force terminating nul char because the man page does not specify whether the resulting buffer is nul terminated in this case */
144 port_ptr->name[g_max_jack_port_name_size] = 0;
145 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);
149 struct a2j_port *
150 a2j_port_create(
151 struct a2j * self,
152 int type,
153 snd_seq_addr_t addr,
154 const snd_seq_port_info_t * info)
156 struct a2j_port *port;
157 int err;
158 int client;
159 snd_seq_client_info_t * client_info_ptr;
160 int jack_caps;
161 struct a2j_stream * stream_ptr;
163 stream_ptr = &self->stream[type];
165 err = snd_seq_client_info_malloc(&client_info_ptr);
166 if (err != 0)
168 a2j_error("Failed to allocate client info");
169 goto fail;
172 client = snd_seq_port_info_get_client(info);
174 err = snd_seq_get_any_client_info(self->seq, client, client_info_ptr);
175 if (err != 0)
177 a2j_error("Failed to get client info");
178 goto fail_free_client_info;
181 a2j_debug("client name: '%s'", snd_seq_client_info_get_name(client_info_ptr));
182 a2j_debug("port name: '%s'", snd_seq_port_info_get_name(info));
184 port = calloc(1, sizeof(struct a2j_port) + g_max_jack_port_name_size);
185 if (!port)
187 goto fail_free_client_info;
190 port->a2j_ptr = self;
192 port->jack_port = JACK_INVALID_PORT;
193 port->remote = addr;
195 a2j_port_fill_name(port, type, client_info_ptr, info, !g_disable_port_uniqueness);
197 /* Add port to list early, before registering to JACK, so map functionality is guaranteed to work during port registration */
198 list_add_tail(&port->siblings, &stream_ptr->list);
200 if (type == A2J_PORT_CAPTURE)
202 jack_caps = JackPortIsOutput;
204 else
206 jack_caps = JackPortIsInput;
209 /* mark anything that looks like a hardware port as physical&terminal */
210 if (snd_seq_port_info_get_type(info) & (SND_SEQ_PORT_TYPE_HARDWARE|SND_SEQ_PORT_TYPE_PORT|SND_SEQ_PORT_TYPE_SPECIFIC))
212 jack_caps |= JackPortIsPhysical|JackPortIsTerminal;
215 port->jack_port = jack_port_register(self->jack_client, port->name, JACK_DEFAULT_MIDI_TYPE, jack_caps, 0);
216 if (port->jack_port == JACK_INVALID_PORT)
218 a2j_error("jack_port_register() failed for '%s'", port->name);
219 goto fail_free_port;
222 if (type == A2J_PORT_CAPTURE)
224 err = a2j_alsa_connect_from(self, port->remote.client, port->remote.port);
226 else
228 err = snd_seq_connect_to(self->seq, self->port_id, port->remote.client, port->remote.port);
229 if (err != 0)
231 a2j_error("snd_seq_connect_to() for %d:%d failed with error %d", (int)port->remote.client, (int)port->remote.port, err);
235 if (err)
237 a2j_info("port skipped: %s", port->name);
238 goto fail_free_port;
241 port->inbound_events = jack_ringbuffer_create(MAX_EVENT_SIZE*16);
243 a2j_info("port created: %s", port->name);
244 snd_seq_client_info_free(client_info_ptr);
245 return port;
247 fail_free_port:
248 list_del(&port->siblings);
250 a2j_port_free(port);
252 fail_free_client_info:
253 snd_seq_client_info_free(client_info_ptr);
255 fail:
256 return NULL;