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"
37 #define JACKDBUS_IFACE_PATCHBAY "org.jackaudio.JackPatchbay"
39 #define JACKDBUS_PORT_FLAG_INPUT 0x00000001
40 #define JACKDBUS_PORT_FLAG_OUTPUT 0x00000002
41 #define JACKDBUS_PORT_FLAG_PHYSICAL 0x00000004
42 #define JACKDBUS_PORT_FLAG_CAN_MONITOR 0x00000008
43 #define JACKDBUS_PORT_FLAG_TERMINAL 0x00000010
45 #define JACKDBUS_PORT_TYPE_AUDIO 0
46 #define JACKDBUS_PORT_TYPE_MIDI 1
50 struct list_head siblings
;
52 void (* clear
)(void * context
);
53 void (* client_appeared
)(void * context
, uint64_t id
, const char * name
);
54 void (* client_disappeared
)(void * context
, uint64_t id
);
55 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
);
56 void (* port_disappeared
)(void * context
, uint64_t client_id
, uint64_t port_id
);
57 void (* ports_connected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
);
58 void (* ports_disconnected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
);
63 struct list_head monitors
;
70 static DBusHandlerResult
message_hook(DBusConnection
*, DBusMessage
*, void *);
72 static void clear(struct graph
* graph_ptr
)
74 struct list_head
* node_ptr
;
75 struct monitor
* monitor_ptr
;
77 list_for_each(node_ptr
, &graph_ptr
->monitors
)
79 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
80 monitor_ptr
->clear(monitor_ptr
->context
);
84 static void client_appeared(struct graph
* graph_ptr
, uint64_t id
, const char * name
)
86 struct list_head
* node_ptr
;
87 struct monitor
* monitor_ptr
;
89 list_for_each(node_ptr
, &graph_ptr
->monitors
)
91 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
92 monitor_ptr
->client_appeared(monitor_ptr
->context
, id
, name
);
96 static void client_disappeared(struct graph
* graph_ptr
, uint64_t id
)
98 struct list_head
* node_ptr
;
99 struct monitor
* monitor_ptr
;
101 list_for_each(node_ptr
, &graph_ptr
->monitors
)
103 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
104 monitor_ptr
->client_disappeared(monitor_ptr
->context
, id
);
111 struct graph
* graph_ptr
,
114 const char * port_name
,
118 struct list_head
* node_ptr
;
119 struct monitor
* monitor_ptr
;
124 if (port_type
!= JACKDBUS_PORT_TYPE_AUDIO
&& port_type
!= JACKDBUS_PORT_TYPE_MIDI
)
126 lash_error("Unknown JACK D-Bus port type %d", (unsigned int)port_type
);
130 is_input
= port_flags
& JACKDBUS_PORT_FLAG_INPUT
;
131 is_terminal
= port_flags
& JACKDBUS_PORT_FLAG_TERMINAL
;
132 is_midi
= port_type
== JACKDBUS_PORT_TYPE_MIDI
;
134 list_for_each(node_ptr
, &graph_ptr
->monitors
)
136 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
137 monitor_ptr
->port_appeared(monitor_ptr
->context
, client_id
, port_id
, port_name
, is_input
, is_terminal
, is_midi
);
144 struct graph
* graph_ptr
,
148 struct list_head
* node_ptr
;
149 struct monitor
* monitor_ptr
;
151 list_for_each(node_ptr
, &graph_ptr
->monitors
)
153 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
154 monitor_ptr
->port_disappeared(monitor_ptr
->context
, client_id
, port_id
);
161 struct graph
* graph_ptr
,
167 struct list_head
* node_ptr
;
168 struct monitor
* monitor_ptr
;
170 list_for_each(node_ptr
, &graph_ptr
->monitors
)
172 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
173 monitor_ptr
->ports_connected(monitor_ptr
->context
, client1_id
, port1_id
, client2_id
, port2_id
);
180 struct graph
* graph_ptr
,
186 struct list_head
* node_ptr
;
187 struct monitor
* monitor_ptr
;
189 list_for_each(node_ptr
, &graph_ptr
->monitors
)
191 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
192 monitor_ptr
->ports_disconnected(monitor_ptr
->context
, client1_id
, port1_id
, client2_id
, port2_id
);
196 static void refresh_internal(struct graph
* graph_ptr
, bool force
)
198 DBusMessage
* reply_ptr
;
199 DBusMessageIter iter
;
200 dbus_uint64_t version
;
201 const char * reply_signature
;
202 DBusMessageIter clients_array_iter
;
203 DBusMessageIter client_struct_iter
;
204 DBusMessageIter ports_array_iter
;
205 DBusMessageIter port_struct_iter
;
206 DBusMessageIter connections_array_iter
;
207 DBusMessageIter connection_struct_iter
;
208 dbus_uint64_t client_id
;
209 const char *client_name
;
210 dbus_uint64_t port_id
;
211 const char *port_name
;
212 dbus_uint32_t port_flags
;
213 dbus_uint32_t port_type
;
214 dbus_uint64_t client2_id
;
215 const char *client2_name
;
216 dbus_uint64_t port2_id
;
217 const char *port2_name
;
218 dbus_uint64_t connection_id
;
220 lash_info("refresh_internal() called");
224 version
= 0; // workaround module split/join stupidity
228 version
= graph_ptr
->version
;
231 if (!dbus_call_simple(graph_ptr
->service
, graph_ptr
->object
, JACKDBUS_IFACE_PATCHBAY
, "GetGraph", "t", &version
, NULL
, &reply_ptr
))
233 lash_error("GetGraph() failed.");
237 reply_signature
= dbus_message_get_signature(reply_ptr
);
239 if (strcmp(reply_signature
, "ta(tsa(tsuu))a(tstststst)") != 0)
241 lash_error("GetGraph() reply signature mismatch. '%s'", reply_signature
);
245 dbus_message_iter_init(reply_ptr
, &iter
);
247 //info_msg((std::string)"version " + (char)dbus_message_iter_get_arg_type(&iter));
248 dbus_message_iter_get_basic(&iter
, &version
);
249 dbus_message_iter_next(&iter
);
251 if (!force
&& version
<= graph_ptr
->version
)
258 //info_msg(str(boost::format("got new graph version %llu") % version));
259 graph_ptr
->version
= version
;
261 //info_msg((std::string)"clients " + (char)dbus_message_iter_get_arg_type(&iter));
263 for (dbus_message_iter_recurse(&iter
, &clients_array_iter
);
264 dbus_message_iter_get_arg_type(&clients_array_iter
) != DBUS_TYPE_INVALID
;
265 dbus_message_iter_next(&clients_array_iter
))
267 //info_msg((std::string)"a client " + (char)dbus_message_iter_get_arg_type(&clients_array_iter));
268 dbus_message_iter_recurse(&clients_array_iter
, &client_struct_iter
);
270 dbus_message_iter_get_basic(&client_struct_iter
, &client_id
);
271 dbus_message_iter_next(&client_struct_iter
);
273 dbus_message_iter_get_basic(&client_struct_iter
, &client_name
);
274 dbus_message_iter_next(&client_struct_iter
);
276 //info_msg((std::string)"client '" + client_name + "'");
278 client_appeared(graph_ptr
, client_id
, client_name
);
280 for (dbus_message_iter_recurse(&client_struct_iter
, &ports_array_iter
);
281 dbus_message_iter_get_arg_type(&ports_array_iter
) != DBUS_TYPE_INVALID
;
282 dbus_message_iter_next(&ports_array_iter
))
284 //info_msg((std::string)"a port " + (char)dbus_message_iter_get_arg_type(&ports_array_iter));
285 dbus_message_iter_recurse(&ports_array_iter
, &port_struct_iter
);
287 dbus_message_iter_get_basic(&port_struct_iter
, &port_id
);
288 dbus_message_iter_next(&port_struct_iter
);
290 dbus_message_iter_get_basic(&port_struct_iter
, &port_name
);
291 dbus_message_iter_next(&port_struct_iter
);
293 dbus_message_iter_get_basic(&port_struct_iter
, &port_flags
);
294 dbus_message_iter_next(&port_struct_iter
);
296 dbus_message_iter_get_basic(&port_struct_iter
, &port_type
);
297 dbus_message_iter_next(&port_struct_iter
);
299 //info_msg((std::string)"port: " + port_name);
301 port_appeared(graph_ptr
, client_id
, port_id
, port_name
, port_flags
, port_type
);
304 dbus_message_iter_next(&client_struct_iter
);
307 dbus_message_iter_next(&iter
);
309 for (dbus_message_iter_recurse(&iter
, &connections_array_iter
);
310 dbus_message_iter_get_arg_type(&connections_array_iter
) != DBUS_TYPE_INVALID
;
311 dbus_message_iter_next(&connections_array_iter
))
313 //info_msg((std::string)"a connection " + (char)dbus_message_iter_get_arg_type(&connections_array_iter));
314 dbus_message_iter_recurse(&connections_array_iter
, &connection_struct_iter
);
316 dbus_message_iter_get_basic(&connection_struct_iter
, &client_id
);
317 dbus_message_iter_next(&connection_struct_iter
);
319 dbus_message_iter_get_basic(&connection_struct_iter
, &client_name
);
320 dbus_message_iter_next(&connection_struct_iter
);
322 dbus_message_iter_get_basic(&connection_struct_iter
, &port_id
);
323 dbus_message_iter_next(&connection_struct_iter
);
325 dbus_message_iter_get_basic(&connection_struct_iter
, &port_name
);
326 dbus_message_iter_next(&connection_struct_iter
);
328 dbus_message_iter_get_basic(&connection_struct_iter
, &client2_id
);
329 dbus_message_iter_next(&connection_struct_iter
);
331 dbus_message_iter_get_basic(&connection_struct_iter
, &client2_name
);
332 dbus_message_iter_next(&connection_struct_iter
);
334 dbus_message_iter_get_basic(&connection_struct_iter
, &port2_id
);
335 dbus_message_iter_next(&connection_struct_iter
);
337 dbus_message_iter_get_basic(&connection_struct_iter
, &port2_name
);
338 dbus_message_iter_next(&connection_struct_iter
);
340 dbus_message_iter_get_basic(&connection_struct_iter
, &connection_id
);
341 dbus_message_iter_next(&connection_struct_iter
);
343 //info_msg(str(boost::format("connection(%llu) %s(%llu):%s(%llu) <-> %s(%llu):%s(%llu)") %
354 ports_connected(graph_ptr
, client_id
, port_id
, client2_id
, port2_id
);
358 dbus_message_unref(reply_ptr
);
364 const char * service
,
366 graph_handle
* graph_handle_ptr
)
368 struct graph
* graph_ptr
;
370 graph_ptr
= malloc(sizeof(struct graph
));
371 if (graph_ptr
== NULL
)
373 lash_error("malloc() failed to allocate struct graph");
377 graph_ptr
->service
= strdup(service
);
378 if (graph_ptr
->service
== NULL
)
380 lash_error("strdup() failed too duplicate service name '%s'", service
);
384 graph_ptr
->object
= strdup(object
);
385 if (graph_ptr
->object
== NULL
)
387 lash_error("strdup() failed too duplicate object name '%s'", object
);
391 INIT_LIST_HEAD(&graph_ptr
->monitors
);
393 graph_ptr
->version
= 0;
394 graph_ptr
->active
= false;
396 *graph_handle_ptr
= (graph_handle
)graph_ptr
;
401 free(graph_ptr
->service
);
410 #define graph_ptr ((struct graph *)graph)
416 assert(list_empty(&graph_ptr
->monitors
));
418 free(graph_ptr
->object
);
419 free(graph_ptr
->service
);
428 const char ** signal
;
430 const char * patchbay_signals
[] = {
439 if (list_empty(&graph_ptr
->monitors
))
441 lash_error("no monitors to activate");
445 if (graph_ptr
->active
)
447 lash_error("graph already active");
451 for (signal
= patchbay_signals
; *signal
!= NULL
; signal
++)
456 "type='signal',sender='%s',path='%s',interface='" JACKDBUS_IFACE_PATCHBAY
"',member='%s'",
461 dbus_bus_add_match(g_dbus_connection
, rule
, &g_dbus_error
);
462 if (dbus_error_is_set(&g_dbus_error
))
464 lash_error("Failed to add D-Bus match rule: %s", g_dbus_error
.message
);
465 dbus_error_free(&g_dbus_error
);
470 dbus_connection_add_filter(g_dbus_connection
, message_hook
, graph_ptr
, NULL
);
472 graph_ptr
->active
= true;
474 refresh_internal(graph_ptr
, true);
483 void (* clear
)(void * context
),
484 void (* client_appeared
)(void * context
, uint64_t id
, const char * name
),
485 void (* client_disappeared
)(void * context
, uint64_t id
),
486 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
),
487 void (* port_disappeared
)(void * context
, uint64_t client_id
, uint64_t port_id
),
488 void (* ports_connected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
),
489 void (* ports_disconnected
)(void * context
, uint64_t client1_id
, uint64_t port1_id
, uint64_t client2_id
, uint64_t port2_id
))
491 struct monitor
* monitor_ptr
;
493 if (graph_ptr
->active
)
498 monitor_ptr
= malloc(sizeof(struct monitor
));
499 if (monitor_ptr
== NULL
)
501 lash_error("malloc() failed to allocate struct monitor");
505 monitor_ptr
->context
= context
;
506 monitor_ptr
->clear
= clear
;
507 monitor_ptr
->client_appeared
= client_appeared
;
508 monitor_ptr
->client_disappeared
= client_disappeared
;
509 monitor_ptr
->port_appeared
= port_appeared
;
510 monitor_ptr
->port_disappeared
= port_disappeared
;
511 monitor_ptr
->ports_connected
= ports_connected
;
512 monitor_ptr
->ports_disconnected
= ports_disconnected
;
514 list_add_tail(&monitor_ptr
->siblings
, &graph_ptr
->monitors
);
524 struct list_head
* node_ptr
;
525 struct monitor
* monitor_ptr
;
527 list_for_each(node_ptr
, &graph_ptr
->monitors
)
529 monitor_ptr
= list_entry(node_ptr
, struct monitor
, siblings
);
530 if (monitor_ptr
->context
== context
)
532 list_del(&monitor_ptr
->siblings
);
547 if (!dbus_call_simple(graph_ptr
->service
, graph_ptr
->object
, JACKDBUS_IFACE_PATCHBAY
, "ConnectPortsByID", "tt", &port1_id
, &port2_id
, ""))
549 lash_error("ConnectPortsByID() failed.");
554 graph_disconnect_ports(
559 if (!dbus_call_simple(graph_ptr
->service
, graph_ptr
->object
, JACKDBUS_IFACE_PATCHBAY
, "DisconnectPortsByID", "tt", &port1_id
, &port2_id
, ""))
561 lash_error("DisconnectPortsByID() failed.");
568 DBusConnection
* connection
,
569 DBusMessage
* message
,
572 const char * object_path
;
573 dbus_uint64_t new_graph_version
;
574 dbus_uint64_t client_id
;
575 const char *client_name
;
576 dbus_uint64_t port_id
;
577 const char *port_name
;
578 dbus_uint32_t port_flags
;
579 dbus_uint32_t port_type
;
580 dbus_uint64_t client2_id
;
581 const char *client2_name
;
582 dbus_uint64_t port2_id
;
583 const char *port2_name
;
584 dbus_uint64_t connection_id
;
586 object_path
= dbus_message_get_path(message
);
587 if (object_path
== NULL
|| strcmp(object_path
, graph_ptr
->object
) != 0)
589 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
592 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "ClientAppeared"))
594 if (!dbus_message_get_args(
597 DBUS_TYPE_UINT64
, &new_graph_version
,
598 DBUS_TYPE_UINT64
, &client_id
,
599 DBUS_TYPE_STRING
, &client_name
,
602 lash_error("dbus_message_get_args() failed to extract ClientAppeared signal arguments (%s)", g_dbus_error
.message
);
603 dbus_error_free(&g_dbus_error
);
604 return DBUS_HANDLER_RESULT_HANDLED
;
607 //lash_info("ClientAppeared, %s(%llu)", client_name, client_id);
609 client_appeared(graph_ptr
, client_id
, client_name
);
611 return DBUS_HANDLER_RESULT_HANDLED
;
614 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "ClientDisappeared"))
616 if (!dbus_message_get_args(
619 DBUS_TYPE_UINT64
, &new_graph_version
,
620 DBUS_TYPE_UINT64
, &client_id
,
621 DBUS_TYPE_STRING
, &client_name
,
624 lash_error("dbus_message_get_args() failed to extract ClientDisappeared signal arguments (%s)", g_dbus_error
.message
);
625 dbus_error_free(&g_dbus_error
);
626 return DBUS_HANDLER_RESULT_HANDLED
;
629 //lash_info("ClientDisappeared, %s(%llu)", client_name, client_id);
631 client_disappeared(graph_ptr
, client_id
);
633 return DBUS_HANDLER_RESULT_HANDLED
;
636 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortAppeared"))
638 if (!dbus_message_get_args(
641 DBUS_TYPE_UINT64
, &new_graph_version
,
642 DBUS_TYPE_UINT64
, &client_id
,
643 DBUS_TYPE_STRING
, &client_name
,
644 DBUS_TYPE_UINT64
, &port_id
,
645 DBUS_TYPE_STRING
, &port_name
,
646 DBUS_TYPE_UINT32
, &port_flags
,
647 DBUS_TYPE_UINT32
, &port_type
,
650 lash_error("dbus_message_get_args() failed to extract PortAppeared signal arguments (%s)", g_dbus_error
.message
);
651 dbus_error_free(&g_dbus_error
);
652 return DBUS_HANDLER_RESULT_HANDLED
;
655 //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));
657 port_appeared(graph_ptr
, client_id
, port_id
, port_name
, port_flags
, port_type
);
659 return DBUS_HANDLER_RESULT_HANDLED
;
662 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortDisappeared"))
664 if (!dbus_message_get_args(
667 DBUS_TYPE_UINT64
, &new_graph_version
,
668 DBUS_TYPE_UINT64
, &client_id
,
669 DBUS_TYPE_STRING
, &client_name
,
670 DBUS_TYPE_UINT64
, &port_id
,
671 DBUS_TYPE_STRING
, &port_name
,
674 lash_error("dbus_message_get_args() failed to extract PortDisappeared signal arguments (%s)", g_dbus_error
.message
);
675 dbus_error_free(&g_dbus_error
);
676 return DBUS_HANDLER_RESULT_HANDLED
;
679 //me->info_msg(str(boost::format("PortDisappeared, %s(%llu):%s(%llu)") % client_name % client_id % port_name % port_id));
681 port_disappeared(graph_ptr
, client_id
, port_id
);
683 return DBUS_HANDLER_RESULT_HANDLED
;
686 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortsConnected"))
688 if (!dbus_message_get_args(
691 DBUS_TYPE_UINT64
, &new_graph_version
,
692 DBUS_TYPE_UINT64
, &client_id
,
693 DBUS_TYPE_STRING
, &client_name
,
694 DBUS_TYPE_UINT64
, &port_id
,
695 DBUS_TYPE_STRING
, &port_name
,
696 DBUS_TYPE_UINT64
, &client2_id
,
697 DBUS_TYPE_STRING
, &client2_name
,
698 DBUS_TYPE_UINT64
, &port2_id
,
699 DBUS_TYPE_STRING
, &port2_name
,
700 DBUS_TYPE_UINT64
, &connection_id
,
703 lash_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", g_dbus_error
.message
);
704 dbus_error_free(&g_dbus_error
);
705 return DBUS_HANDLER_RESULT_HANDLED
;
708 ports_connected(graph_ptr
, client_id
, port_id
, client2_id
, port2_id
);
710 return DBUS_HANDLER_RESULT_HANDLED
;
713 if (dbus_message_is_signal(message
, JACKDBUS_IFACE_PATCHBAY
, "PortsDisconnected"))
715 if (!dbus_message_get_args(
718 DBUS_TYPE_UINT64
, &new_graph_version
,
719 DBUS_TYPE_UINT64
, &client_id
,
720 DBUS_TYPE_STRING
, &client_name
,
721 DBUS_TYPE_UINT64
, &port_id
,
722 DBUS_TYPE_STRING
, &port_name
,
723 DBUS_TYPE_UINT64
, &client2_id
,
724 DBUS_TYPE_STRING
, &client2_name
,
725 DBUS_TYPE_UINT64
, &port2_id
,
726 DBUS_TYPE_STRING
, &port2_name
,
727 DBUS_TYPE_UINT64
, &connection_id
,
730 lash_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", g_dbus_error
.message
);
731 dbus_error_free(&g_dbus_error
);
732 return DBUS_HANDLER_RESULT_HANDLED
;
735 ports_disconnected(graph_ptr
, client_id
, port_id
, client2_id
, port2_id
);
737 return DBUS_HANDLER_RESULT_HANDLED
;
740 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;