1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * ALSA SEQ < - > JACK MIDI bridge
5 * Copyright (c) 2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <dbus/dbus.h>
23 #include <alsa/asoundlib.h>
24 #include <jack/jack.h>
25 #include <jack/midiport.h>
26 #include <jack/ringbuffer.h>
28 #include "dbus_internal.h"
33 #include "port_thread.h"
36 #define INTERFACE_NAME "org.gna.home.a2jmidid.control"
39 a2j_dbus_signal_emit_bridge_started()
41 a2j_dbus_signal("/", INTERFACE_NAME
, "bridge_started", DBUS_TYPE_INVALID
);
45 a2j_dbus_signal_emit_bridge_stopped()
47 a2j_dbus_signal("/", INTERFACE_NAME
, "bridge_stopped", DBUS_TYPE_INVALID
);
53 struct a2j_dbus_method_call
* call_ptr
)
55 g_keep_walking
= false;
56 a2j_dbus_construct_method_return_void(call_ptr
);
59 static void a2j_dbus_set_hw_export(struct a2j_dbus_method_call
* call_ptr
)
62 dbus_bool_t hw_export
;
66 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_BRIDGE_RUNNING
, "Bridge is started");
70 dbus_error_init(&error
);
72 if (!dbus_message_get_args(
75 DBUS_TYPE_BOOLEAN
, &hw_export
,
78 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\"", call_ptr
->method_name
);
79 dbus_error_free(&error
);
83 g_a2j_export_hw_ports
= hw_export
;
85 a2j_info("Hardware ports %s be exported.", g_a2j_export_hw_ports
? "will": "will not");
87 a2j_dbus_construct_method_return_void(call_ptr
);
93 struct a2j_dbus_method_call
* call_ptr
)
97 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_GENERIC
, "a2j_start() failed.");
101 a2j_dbus_construct_method_return_void(call_ptr
);
108 struct a2j_dbus_method_call
* call_ptr
)
112 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_GENERIC
, "a2j_stop() failed.");
116 a2j_dbus_construct_method_return_void(call_ptr
);
123 struct a2j_dbus_method_call
* call_ptr
)
125 dbus_bool_t is_started
;
127 is_started
= a2j_is_started();
129 a2j_dbus_construct_method_return_single(
137 a2j_dbus_get_jack_client_name(
138 struct a2j_dbus_method_call
* call_ptr
)
140 const char * jack_client_name
;
142 jack_client_name
= A2J_JACK_CLIENT_NAME
;
144 a2j_dbus_construct_method_return_single(
152 a2j_dbus_map_alsa_to_jack_port(
153 struct a2j_dbus_method_call
* call_ptr
)
156 dbus_uint32_t client_id
;
157 dbus_uint32_t port_id
;
158 dbus_bool_t map_playback
;
160 struct a2j_port
* port_ptr
;
161 const char * direction_string
;
162 struct a2j_stream
* stream_ptr
;
163 const char * jack_port
;
165 dbus_error_init(&error
);
167 if (!dbus_message_get_args(
170 DBUS_TYPE_UINT32
, &client_id
,
171 DBUS_TYPE_UINT32
, &port_id
,
172 DBUS_TYPE_BOOLEAN
, &map_playback
,
175 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\"", call_ptr
->method_name
);
176 dbus_error_free(&error
);
180 if (!a2j_is_started())
182 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_BRIDGE_NOT_RUNNING
, "Bridge not started");
186 addr
.client
= client_id
;
191 stream_ptr
= g_a2j
->stream
+ A2J_PORT_PLAYBACK
;
192 direction_string
= "playback";
196 stream_ptr
= g_a2j
->stream
+ A2J_PORT_CAPTURE
;
197 direction_string
= "capture";
200 port_ptr
= a2j_find_port_by_addr(stream_ptr
, addr
);
201 if (port_ptr
== NULL
)
203 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_UNKNOWN_PORT
, "Unknown ALSA sequencer port %u:%u (%s)", (unsigned int)client_id
, (unsigned int)port_id
, direction_string
);
207 jack_port
= port_ptr
->name
;
209 a2j_info("map %u:%u (%s) -> '%s'", (unsigned int)client_id
, (unsigned int)port_id
, direction_string
, jack_port
);
211 a2j_dbus_construct_method_return_single(
219 a2j_dbus_map_jack_port_to_alsa(
220 struct a2j_dbus_method_call
* call_ptr
)
222 snd_seq_client_info_t
* client_info_ptr
;
223 snd_seq_port_info_t
* port_info_ptr
;
225 const char * jack_port
;
226 struct a2j_port
* port_ptr
;
227 const char * client_name
;
228 const char * port_name
;
229 dbus_uint32_t client_id
;
230 dbus_uint32_t port_id
;
231 DBusMessageIter iter
;
234 snd_seq_client_info_alloca(&client_info_ptr
);
235 snd_seq_port_info_alloca(&port_info_ptr
);
237 dbus_error_init(&error
);
239 if (!dbus_message_get_args(
242 DBUS_TYPE_STRING
, &jack_port
,
245 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_INVALID_ARGS
, "Invalid arguments to method \"%s\"", call_ptr
->method_name
);
246 dbus_error_free(&error
);
250 if (!a2j_is_started())
252 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_BRIDGE_NOT_RUNNING
, "Bridge not started");
256 port_ptr
= a2j_find_port_by_jack_port_name(g_a2j
->stream
+ A2J_PORT_CAPTURE
, jack_port
);
257 if (port_ptr
== NULL
)
259 port_ptr
= a2j_find_port_by_jack_port_name(g_a2j
->stream
+ A2J_PORT_PLAYBACK
, jack_port
);
260 if (port_ptr
== NULL
)
262 a2j_dbus_error(call_ptr
, A2J_DBUS_ERROR_UNKNOWN_PORT
, "Unknown JACK port '%s'", jack_port
);
267 if (snd_seq_get_any_client_info(g_a2j
->seq
, port_ptr
->remote
.client
, client_info_ptr
) >= 0)
269 client_name
= snd_seq_client_info_get_name(client_info_ptr
);
276 if (snd_seq_get_any_port_info(g_a2j
->seq
, port_ptr
->remote
.client
, port_ptr
->remote
.port
, port_info_ptr
) >= 0)
278 port_name
= snd_seq_port_info_get_name(port_info_ptr
);
285 a2j_info("map '%s' -> %u:%u ('%s':'%s')", jack_port
, (unsigned int)port_ptr
->remote
.client
, (unsigned int)port_ptr
->remote
.port
, client_name
, port_name
);
287 client_id
= port_ptr
->remote
.client
;
288 port_id
= port_ptr
->remote
.port
;
290 call_ptr
->reply
= dbus_message_new_method_return(call_ptr
->message
);
291 if (call_ptr
->reply
== NULL
)
296 dbus_message_iter_init_append(call_ptr
->reply
, &iter
);
298 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_UINT32
, &client_id
))
303 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_UINT32
, &port_id
))
308 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &client_name
))
313 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &port_name
))
321 dbus_message_unref(call_ptr
->reply
);
322 call_ptr
->reply
= NULL
;
325 a2j_error("Ran out of memory trying to construct method return");
328 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(exit
)
329 A2J_DBUS_METHOD_ARGUMENTS_END
331 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(start
)
332 A2J_DBUS_METHOD_ARGUMENTS_END
334 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(stop
)
335 A2J_DBUS_METHOD_ARGUMENTS_END
337 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(is_started
)
338 A2J_DBUS_METHOD_ARGUMENT("started", DBUS_TYPE_BOOLEAN_AS_STRING
, A2J_DBUS_DIRECTION_OUT
)
339 A2J_DBUS_METHOD_ARGUMENTS_END
341 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(get_jack_client_name
)
342 A2J_DBUS_METHOD_ARGUMENT("jack_client_name", DBUS_TYPE_STRING_AS_STRING
, A2J_DBUS_DIRECTION_OUT
)
343 A2J_DBUS_METHOD_ARGUMENTS_END
345 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(map_alsa_to_jack_port
)
346 A2J_DBUS_METHOD_ARGUMENT("alsa_client_id", DBUS_TYPE_UINT32_AS_STRING
, A2J_DBUS_DIRECTION_IN
)
347 A2J_DBUS_METHOD_ARGUMENT("alsa_port_id", DBUS_TYPE_UINT32_AS_STRING
, A2J_DBUS_DIRECTION_IN
)
348 A2J_DBUS_METHOD_ARGUMENT("map_playback", DBUS_TYPE_BOOLEAN_AS_STRING
, A2J_DBUS_DIRECTION_IN
)
349 A2J_DBUS_METHOD_ARGUMENT("jack_port_name", DBUS_TYPE_STRING_AS_STRING
, A2J_DBUS_DIRECTION_OUT
)
350 A2J_DBUS_METHOD_ARGUMENTS_END
352 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(map_jack_port_to_alsa
)
353 A2J_DBUS_METHOD_ARGUMENT("jack_port_name", "s", A2J_DBUS_DIRECTION_IN
)
354 A2J_DBUS_METHOD_ARGUMENT("alsa_client_id", DBUS_TYPE_UINT32_AS_STRING
, A2J_DBUS_DIRECTION_OUT
)
355 A2J_DBUS_METHOD_ARGUMENT("alsa_port_id", DBUS_TYPE_UINT32_AS_STRING
, A2J_DBUS_DIRECTION_OUT
)
356 A2J_DBUS_METHOD_ARGUMENT("alsa_client_name", DBUS_TYPE_STRING_AS_STRING
, A2J_DBUS_DIRECTION_OUT
)
357 A2J_DBUS_METHOD_ARGUMENT("alsa_port_name", DBUS_TYPE_STRING_AS_STRING
, A2J_DBUS_DIRECTION_OUT
)
358 A2J_DBUS_METHOD_ARGUMENTS_END
360 A2J_DBUS_METHOD_ARGUMENTS_BEGIN(set_hw_export
)
361 A2J_DBUS_METHOD_ARGUMENT("hw_export", DBUS_TYPE_BOOLEAN_AS_STRING
, A2J_DBUS_DIRECTION_IN
)
362 A2J_DBUS_METHOD_ARGUMENTS_END
364 A2J_DBUS_METHODS_BEGIN
365 A2J_DBUS_METHOD_DESCRIBE(exit
, a2j_dbus_exit
)
366 A2J_DBUS_METHOD_DESCRIBE(start
, a2j_dbus_start
)
367 A2J_DBUS_METHOD_DESCRIBE(stop
, a2j_dbus_stop
)
368 A2J_DBUS_METHOD_DESCRIBE(is_started
, a2j_dbus_is_started
)
369 A2J_DBUS_METHOD_DESCRIBE(get_jack_client_name
, a2j_dbus_get_jack_client_name
)
370 A2J_DBUS_METHOD_DESCRIBE(map_alsa_to_jack_port
, a2j_dbus_map_alsa_to_jack_port
)
371 A2J_DBUS_METHOD_DESCRIBE(map_jack_port_to_alsa
, a2j_dbus_map_jack_port_to_alsa
)
372 A2J_DBUS_METHOD_DESCRIBE(set_hw_export
, a2j_dbus_set_hw_export
)
375 A2J_DBUS_SIGNAL_ARGUMENTS_BEGIN(bridge_started
)
376 A2J_DBUS_SIGNAL_ARGUMENTS_END
378 A2J_DBUS_SIGNAL_ARGUMENTS_BEGIN(bridge_stopped
)
379 A2J_DBUS_SIGNAL_ARGUMENTS_END
381 A2J_DBUS_SIGNALS_BEGIN
382 A2J_DBUS_SIGNAL_DESCRIBE(bridge_started
)
383 A2J_DBUS_SIGNAL_DESCRIBE(bridge_stopped
)
386 A2J_DBUS_IFACE_BEGIN(g_a2j_iface_control
, INTERFACE_NAME
)
387 A2J_DBUS_IFACE_EXPOSE_METHODS
388 A2J_DBUS_IFACE_EXPOSE_SIGNALS