1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation graph object that is backed through D-Bus
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <dbus/dbus.h>
32 #include "graph_proxy.h"
33 #include "common/klist.h"
34 #include "common/debug.h"
35 #include "dbus/helpers.h"
36 #include "dbus_constants.h"
38 #define JACKDBUS_PORT_FLAG_INPUT 0x00000001
39 #define JACKDBUS_PORT_FLAG_OUTPUT 0x00000002
40 #define JACKDBUS_PORT_FLAG_PHYSICAL 0x00000004
41 #define JACKDBUS_PORT_FLAG_CAN_MONITOR 0x00000008
42 #define JACKDBUS_PORT_FLAG_TERMINAL 0x00000010
44 #define JACKDBUS_PORT_TYPE_AUDIO 0
45 #define JACKDBUS_PORT_TYPE_MIDI 1
49 struct list_head siblings
;
51 void (* clear
)(void * context
);
52 void (* client_appeared
)(void * context
, uint64_t id
, const char * name
);
53 void (* client_disappeared
)(void * context
, uint64_t id
);
54 void (* port_appeared
)(void * context
, uint64_t client_id
, uint64_t port_id
, const char * port_name
, bool is_input
, bool is_terminal
, bool is_midi
);
55 void (* port_disappeared
)(void * context
, uint64_t client_id
, uint64_t port_id
);
56 void (* ports_connected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
);
57 void (* ports_disconnected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
);
62 struct list_head monitors
;
69 static DBusHandlerResult
message_hook(DBusConnection
*, DBusMessage
*, void *);
71 static void clear(struct graph
* graph_ptr
)
73 struct list_head
* node_ptr
;
74 struct monitor
* monitor_ptr
;
76 list_for_each(node_ptr
, &graph_ptr
->monitors
)
78 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
79 monitor_ptr
->clear(monitor_ptr
->context
);
83 static void client_appeared(struct graph
* graph_ptr
, uint64_t id
, const char * name
)
85 struct list_head
* node_ptr
;
86 struct monitor
* monitor_ptr
;
88 list_for_each(node_ptr
, &graph_ptr
->monitors
)
90 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
91 monitor_ptr
->client_appeared(monitor_ptr
->context
, id
, name
);
95 static void client_disappeared(struct graph
* graph_ptr
, uint64_t id
)
97 struct list_head
* node_ptr
;
98 struct monitor
* monitor_ptr
;
100 list_for_each(node_ptr
, &graph_ptr
->monitors
)
102 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
103 monitor_ptr
->client_disappeared(monitor_ptr
->context
, id
);
110 struct graph
* graph_ptr
,
113 const char * port_name
,
117 struct list_head
* node_ptr
;
118 struct monitor
* monitor_ptr
;
123 if (port_type
!= JACKDBUS_PORT_TYPE_AUDIO
&& port_type
!= JACKDBUS_PORT_TYPE_MIDI
)
125 lash_error("Unknown JACK D-Bus port type %d", (unsigned int)port_type
);
129 is_input
= port_flags
& JACKDBUS_PORT_FLAG_INPUT
;
130 is_terminal
= port_flags
& JACKDBUS_PORT_FLAG_TERMINAL
;
131 is_midi
= port_type
== JACKDBUS_PORT_TYPE_MIDI
;
133 list_for_each(node_ptr
, &graph_ptr
->monitors
)
135 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
136 monitor_ptr
->port_appeared(monitor_ptr
->context
, client_id
, port_id
, port_name
, is_input
, is_terminal
, is_midi
);
143 struct graph
* graph_ptr
,
147 struct list_head
* node_ptr
;
148 struct monitor
* monitor_ptr
;
150 list_for_each(node_ptr
, &graph_ptr
->monitors
)
152 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
153 monitor_ptr
->port_disappeared(monitor_ptr
->context
, client_id
, port_id
);
160 struct graph
* graph_ptr
,
166 struct list_head
* node_ptr
;
167 struct monitor
* monitor_ptr
;
169 list_for_each(node_ptr
, &graph_ptr
->monitors
)
171 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
172 monitor_ptr
->ports_connected(monitor_ptr
->context
, client1_id
, port1_id
, client2_id
, port2_id
);
179 struct graph
* graph_ptr
,
185 struct list_head
* node_ptr
;
186 struct monitor
* monitor_ptr
;
188 list_for_each(node_ptr
, &graph_ptr
->monitors
)
190 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
191 monitor_ptr
->ports_disconnected(monitor_ptr
->context
, client1_id
, port1_id
, client2_id
, port2_id
);
195 static void refresh_internal(struct graph
* graph_ptr
, bool force
)
197 DBusMessage
* reply_ptr
;
198 DBusMessageIter iter
;
199 dbus_uint64_t version
;
200 const char * reply_signature
;
201 DBusMessageIter clients_array_iter
;
202 DBusMessageIter client_struct_iter
;
203 DBusMessageIter ports_array_iter
;
204 DBusMessageIter port_struct_iter
;
205 DBusMessageIter connections_array_iter
;
206 DBusMessageIter connection_struct_iter
;
207 dbus_uint64_t client_id
;
208 const char *client_name
;
209 dbus_uint64_t port_id
;
210 const char *port_name
;
211 dbus_uint32_t port_flags
;
212 dbus_uint32_t port_type
;
213 dbus_uint64_t client2_id
;
214 const char *client2_name
;
215 dbus_uint64_t port2_id
;
216 const char *port2_name
;
217 dbus_uint64_t connection_id
;
219 lash_info("refresh_internal() called");
223 version
= 0; // workaround module split/join stupidity
227 version
= graph_ptr
->version
;
230 if (!dbus_call_simple(graph_ptr
->service
, graph_ptr
->object
, JACKDBUS_IFACE_PATCHBAY
, "GetGraph", "t", &version
, NULL
, &reply_ptr
))
232 lash_error("GetGraph() failed.");
236 reply_signature
= dbus_message_get_signature(reply_ptr
);
238 if (strcmp(reply_signature
, "ta(tsa(tsuu))a(tstststst)") != 0)
240 lash_error("GetGraph() reply signature mismatch. '%s'", reply_signature
);
244 dbus_message_iter_init(reply_ptr
, &iter
);
246 //info_msg((std::string)"version " + (char)dbus_message_iter_get_arg_type(&iter));
247 dbus_message_iter_get_basic(&iter
, &version
);
248 dbus_message_iter_next(&iter
);
250 if (!force
&& version
<= graph_ptr
->version
)
257 //info_msg(str(boost::format("got new graph version %llu") % version));
258 graph_ptr
->version
= version
;
260 //info_msg((std::string)"clients " + (char)dbus_message_iter_get_arg_type(&iter));
262 for (dbus_message_iter_recurse(&iter
, &clients_array_iter
);
263 dbus_message_iter_get_arg_type(&clients_array_iter
) != DBUS_TYPE_INVALID
;
264 dbus_message_iter_next(&clients_array_iter
))
266 //info_msg((std::string)"a client " + (char)dbus_message_iter_get_arg_type(&clients_array_iter));
267 dbus_message_iter_recurse(&clients_array_iter
, &client_struct_iter
);
269 dbus_message_iter_get_basic(&client_struct_iter
, &client_id
);
270 dbus_message_iter_next(&client_struct_iter
);
272 dbus_message_iter_get_basic(&client_struct_iter
, &client_name
);
273 dbus_message_iter_next(&client_struct_iter
);
275 //info_msg((std::string)"client '" + client_name + "'");
277 client_appeared(graph_ptr
, client_id
, client_name
);
279 for (dbus_message_iter_recurse(&client_struct_iter
, &ports_array_iter
);
280 dbus_message_iter_get_arg_type(&ports_array_iter
) != DBUS_TYPE_INVALID
;
281 dbus_message_iter_next(&ports_array_iter
))
283 //info_msg((std::string)"a port " + (char)dbus_message_iter_get_arg_type(&ports_array_iter));
284 dbus_message_iter_recurse(&ports_array_iter
, &port_struct_iter
);
286 dbus_message_iter_get_basic(&port_struct_iter
, &port_id
);
287 dbus_message_iter_next(&port_struct_iter
);
289 dbus_message_iter_get_basic(&port_struct_iter
, &port_name
);
290 dbus_message_iter_next(&port_struct_iter
);
292 dbus_message_iter_get_basic(&port_struct_iter
, &port_flags
);
293 dbus_message_iter_next(&port_struct_iter
);
295 dbus_message_iter_get_basic(&port_struct_iter
, &port_type
);
296 dbus_message_iter_next(&port_struct_iter
);
298 //info_msg((std::string)"port: " + port_name);
300 port_appeared(graph_ptr
, client_id
, port_id
, port_name
, port_flags
, port_type
);
303 dbus_message_iter_next(&client_struct_iter
);
306 dbus_message_iter_next(&iter
);
308 for (dbus_message_iter_recurse(&iter
, &connections_array_iter
);
309 dbus_message_iter_get_arg_type(&connections_array_iter
) != DBUS_TYPE_INVALID
;
310 dbus_message_iter_next(&connections_array_iter
))
312 //info_msg((std::string)"a connection " + (char)dbus_message_iter_get_arg_type(&connections_array_iter));
313 dbus_message_iter_recurse(&connections_array_iter
, &connection_struct_iter
);
315 dbus_message_iter_get_basic(&connection_struct_iter
, &client_id
);
316 dbus_message_iter_next(&connection_struct_iter
);
318 dbus_message_iter_get_basic(&connection_struct_iter
, &client_name
);
319 dbus_message_iter_next(&connection_struct_iter
);
321 dbus_message_iter_get_basic(&connection_struct_iter
, &port_id
);
322 dbus_message_iter_next(&connection_struct_iter
);
324 dbus_message_iter_get_basic(&connection_struct_iter
, &port_name
);
325 dbus_message_iter_next(&connection_struct_iter
);
327 dbus_message_iter_get_basic(&connection_struct_iter
, &client2_id
);
328 dbus_message_iter_next(&connection_struct_iter
);
330 dbus_message_iter_get_basic(&connection_struct_iter
, &client2_name
);
331 dbus_message_iter_next(&connection_struct_iter
);
333 dbus_message_iter_get_basic(&connection_struct_iter
, &port2_id
);
334 dbus_message_iter_next(&connection_struct_iter
);
336 dbus_message_iter_get_basic(&connection_struct_iter
, &port2_name
);
337 dbus_message_iter_next(&connection_struct_iter
);
339 dbus_message_iter_get_basic(&connection_struct_iter
, &connection_id
);
340 dbus_message_iter_next(&connection_struct_iter
);
342 //info_msg(str(boost::format("connection(%llu) %s(%llu):%s(%llu) <-> %s(%llu):%s(%llu)") %
353 ports_connected(graph_ptr
, client_id
, port_id
, client2_id
, port2_id
);
357 dbus_message_unref(reply_ptr
);
363 const char * service
,
365 graph_handle
* graph_handle_ptr
)
367 struct graph
* graph_ptr
;
369 graph_ptr
= malloc(sizeof(struct graph
));
370 if (graph_ptr
== NULL
)
372 lash_error("malloc() failed to allocate struct graph");
376 graph_ptr
->service
= strdup(service
);
377 if (graph_ptr
->service
== NULL
)
379 lash_error("strdup() failed too duplicate service name '%s'", service
);
383 graph_ptr
->object
= strdup(object
);
384 if (graph_ptr
->object
== NULL
)
386 lash_error("strdup() failed too duplicate object name '%s'", object
);
390 INIT_LIST_HEAD(&graph_ptr
->monitors
);
392 graph_ptr
->version
= 0;
393 graph_ptr
->active
= false;
395 *graph_handle_ptr
= (graph_handle
)graph_ptr
;
400 free(graph_ptr
->service
);
409 #define graph_ptr ((struct graph *)graph)
415 assert(list_empty(&graph_ptr
->monitors
));
417 free(graph_ptr
->object
);
418 free(graph_ptr
->service
);
427 const char ** signal
;
429 const char * patchbay_signals
[] = {
438 if (list_empty(&graph_ptr
->monitors
))
440 lash_error("no monitors to activate");
444 if (graph_ptr
->active
)
446 lash_error("graph already active");
450 for (signal
= patchbay_signals
; *signal
!= NULL
; signal
++)
455 "type='signal',sender='%s',path='%s',interface='" JACKDBUS_IFACE_PATCHBAY
"',member='%s'",
460 dbus_bus_add_match(g_dbus_connection
, rule
, &g_dbus_error
);
461 if (dbus_error_is_set(&g_dbus_error
))
463 lash_error("Failed to add D-Bus match rule: %s", g_dbus_error
.message
);
464 dbus_error_free(&g_dbus_error
);
469 dbus_connection_add_filter(g_dbus_connection
, message_hook
, graph_ptr
, NULL
);
471 graph_ptr
->active
= true;
473 refresh_internal(graph_ptr
, true);
482 void (* clear
)(void * context
),
483 void (* client_appeared
)(void * context
, uint64_t id
, const char * name
),
484 void (* client_disappeared
)(void * context
, uint64_t id
),
485 void (* port_appeared
)(void * context
, uint64_t client_id
, uint64_t port_id
, const char * port_name
, bool is_input
, bool is_terminal
, bool is_midi
),
486 void (* port_disappeared
)(void * context
, uint64_t client_id
, uint64_t port_id
),
487 void (* ports_connected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
),
488 void (* ports_disconnected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
))
490 struct monitor
* monitor_ptr
;
492 if (graph_ptr
->active
)
497 monitor_ptr
= malloc(sizeof(struct monitor
));
498 if (monitor_ptr
== NULL
)
500 lash_error("malloc() failed to allocate struct monitor");
504 monitor_ptr
->context
= context
;
505 monitor_ptr
->clear
= clear
;
506 monitor_ptr
->client_appeared
= client_appeared
;
507 monitor_ptr
->client_disappeared
= client_disappeared
;
508 monitor_ptr
->port_appeared
= port_appeared
;
509 monitor_ptr
->port_disappeared
= port_disappeared
;
510 monitor_ptr
->ports_connected
= ports_connected
;
511 monitor_ptr
->ports_disconnected
= ports_disconnected
;
513 list_add_tail(&monitor_ptr
->siblings
, &graph_ptr
->monitors
);
523 struct list_head
* node_ptr
;
524 struct monitor
* monitor_ptr
;
526 list_for_each(node_ptr
, &graph_ptr
->monitors
)
528 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
529 if (monitor_ptr
->context
== context
)
531 list_del(&monitor_ptr
->siblings
);
546 if (!dbus_call_simple(graph_ptr
->service
, graph_ptr
->object
, JACKDBUS_IFACE_PATCHBAY
, "ConnectPortsByID", "tt", &port1_id
, &port2_id
, ""))
548 lash_error("ConnectPortsByID() failed.");
553 graph_disconnect_ports(
558 if (!dbus_call_simple(graph_ptr
->service
, graph_ptr
->object
, JACKDBUS_IFACE_PATCHBAY
, "DisconnectPortsByID", "tt", &port1_id
, &port2_id
, ""))
560 lash_error("DisconnectPortsByID() failed.");
567 DBusConnection
* connection
,
568 DBusMessage
* message
,
571 const char * object_path
;
572 dbus_uint64_t new_graph_version
;
573 dbus_uint64_t client_id
;
574 const char *client_name
;
575 dbus_uint64_t port_id
;
576 const char *port_name
;
577 dbus_uint32_t port_flags
;
578 dbus_uint32_t port_type
;
579 dbus_uint64_t client2_id
;
580 const char *client2_name
;
581 dbus_uint64_t port2_id
;
582 const char *port2_name
;
583 dbus_uint64_t connection_id
;
585 object_path
= dbus_message_get_path(message
);
586 if (object_path
== NULL
|| strcmp(object_path
, graph_ptr
->object
) != 0)
588 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
591 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "ClientAppeared"))
593 if (!dbus_message_get_args(
596 DBUS_TYPE_UINT64
, &new_graph_version
,
597 DBUS_TYPE_UINT64
, &client_id
,
598 DBUS_TYPE_STRING
, &client_name
,
601 lash_error("dbus_message_get_args() failed to extract ClientAppeared signal arguments (%s)", g_dbus_error
.message
);
602 dbus_error_free(&g_dbus_error
);
603 return DBUS_HANDLER_RESULT_HANDLED
;
606 //lash_info("ClientAppeared, %s(%llu)", client_name, client_id);
608 client_appeared(graph_ptr
, client_id
, client_name
);
610 return DBUS_HANDLER_RESULT_HANDLED
;
613 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "ClientDisappeared"))
615 if (!dbus_message_get_args(
618 DBUS_TYPE_UINT64
, &new_graph_version
,
619 DBUS_TYPE_UINT64
, &client_id
,
620 DBUS_TYPE_STRING
, &client_name
,
623 lash_error("dbus_message_get_args() failed to extract ClientDisappeared signal arguments (%s)", g_dbus_error
.message
);
624 dbus_error_free(&g_dbus_error
);
625 return DBUS_HANDLER_RESULT_HANDLED
;
628 //lash_info("ClientDisappeared, %s(%llu)", client_name, client_id);
630 client_disappeared(graph_ptr
, client_id
);
632 return DBUS_HANDLER_RESULT_HANDLED
;
635 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortAppeared"))
637 if (!dbus_message_get_args(
640 DBUS_TYPE_UINT64
, &new_graph_version
,
641 DBUS_TYPE_UINT64
, &client_id
,
642 DBUS_TYPE_STRING
, &client_name
,
643 DBUS_TYPE_UINT64
, &port_id
,
644 DBUS_TYPE_STRING
, &port_name
,
645 DBUS_TYPE_UINT32
, &port_flags
,
646 DBUS_TYPE_UINT32
, &port_type
,
649 lash_error("dbus_message_get_args() failed to extract PortAppeared signal arguments (%s)", g_dbus_error
.message
);
650 dbus_error_free(&g_dbus_error
);
651 return DBUS_HANDLER_RESULT_HANDLED
;
654 //me->info_msg(str(boost::format("PortAppeared, %s(%llu):%s(%llu), %lu, %lu") % client_name % client_id % port_name % port_id % port_flags % port_type));
656 port_appeared(graph_ptr
, client_id
, port_id
, port_name
, port_flags
, port_type
);
658 return DBUS_HANDLER_RESULT_HANDLED
;
661 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortDisappeared"))
663 if (!dbus_message_get_args(
666 DBUS_TYPE_UINT64
, &new_graph_version
,
667 DBUS_TYPE_UINT64
, &client_id
,
668 DBUS_TYPE_STRING
, &client_name
,
669 DBUS_TYPE_UINT64
, &port_id
,
670 DBUS_TYPE_STRING
, &port_name
,
673 lash_error("dbus_message_get_args() failed to extract PortDisappeared signal arguments (%s)", g_dbus_error
.message
);
674 dbus_error_free(&g_dbus_error
);
675 return DBUS_HANDLER_RESULT_HANDLED
;
678 //me->info_msg(str(boost::format("PortDisappeared, %s(%llu):%s(%llu)") % client_name % client_id % port_name % port_id));
680 port_disappeared(graph_ptr
, client_id
, port_id
);
682 return DBUS_HANDLER_RESULT_HANDLED
;
685 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortsConnected"))
687 if (!dbus_message_get_args(
690 DBUS_TYPE_UINT64
, &new_graph_version
,
691 DBUS_TYPE_UINT64
, &client_id
,
692 DBUS_TYPE_STRING
, &client_name
,
693 DBUS_TYPE_UINT64
, &port_id
,
694 DBUS_TYPE_STRING
, &port_name
,
695 DBUS_TYPE_UINT64
, &client2_id
,
696 DBUS_TYPE_STRING
, &client2_name
,
697 DBUS_TYPE_UINT64
, &port2_id
,
698 DBUS_TYPE_STRING
, &port2_name
,
699 DBUS_TYPE_UINT64
, &connection_id
,
702 lash_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", g_dbus_error
.message
);
703 dbus_error_free(&g_dbus_error
);
704 return DBUS_HANDLER_RESULT_HANDLED
;
707 ports_connected(graph_ptr
, client_id
, port_id
, client2_id
, port2_id
);
709 return DBUS_HANDLER_RESULT_HANDLED
;
712 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortsDisconnected"))
714 if (!dbus_message_get_args(
717 DBUS_TYPE_UINT64
, &new_graph_version
,
718 DBUS_TYPE_UINT64
, &client_id
,
719 DBUS_TYPE_STRING
, &client_name
,
720 DBUS_TYPE_UINT64
, &port_id
,
721 DBUS_TYPE_STRING
, &port_name
,
722 DBUS_TYPE_UINT64
, &client2_id
,
723 DBUS_TYPE_STRING
, &client2_name
,
724 DBUS_TYPE_UINT64
, &port2_id
,
725 DBUS_TYPE_STRING
, &port2_name
,
726 DBUS_TYPE_UINT64
, &connection_id
,
729 lash_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", g_dbus_error
.message
);
730 dbus_error_free(&g_dbus_error
);
731 return DBUS_HANDLER_RESULT_HANDLED
;
734 ports_disconnected(graph_ptr
, client_id
, port_id
, client2_id
, port2_id
);
736 return DBUS_HANDLER_RESULT_HANDLED
;
739 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;