Add missing header text
[ladish.git] / proxies / graph_proxy.c
blob92d55807c5f60a1d4e9f291cae2ff46af042979f
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009,2010,2011 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 "graph_proxy.h"
29 struct monitor
31 struct list_head siblings;
32 void * context;
33 void (* clear)(void * context);
34 void (* client_appeared)(void * context, uint64_t id, const char * name);
35 void (* client_renamed)(void * context, uint64_t client_id, const char * old_client_name, const char * new_client_name);
36 void (* client_disappeared)(void * context, uint64_t id);
37 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);
38 void (* port_renamed)(void * context, uint64_t client_id, uint64_t port_id, const char * old_port_name, const char * new_port_name);
39 void (* port_disappeared)(void * context, uint64_t client_id, uint64_t port_id);
40 void (* ports_connected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id);
41 void (* ports_disconnected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id);
44 struct graph
46 struct list_head monitors;
47 char * service;
48 char * object;
49 uint64_t version;
50 bool active;
51 bool graph_dict_supported;
52 bool graph_manager_supported;
55 static struct cdbus_signal_hook g_signal_hooks[];
57 static void clear(struct graph * graph_ptr)
59 struct list_head * node_ptr;
60 struct monitor * monitor_ptr;
62 list_for_each(node_ptr, &graph_ptr->monitors)
64 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
65 monitor_ptr->clear(monitor_ptr->context);
69 static void client_appeared(struct graph * graph_ptr, uint64_t id, const char * name)
71 struct list_head * node_ptr;
72 struct monitor * monitor_ptr;
74 list_for_each(node_ptr, &graph_ptr->monitors)
76 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
77 monitor_ptr->client_appeared(monitor_ptr->context, id, name);
81 static
82 void
83 client_renamed(
84 struct graph * graph_ptr,
85 uint64_t client_id,
86 const char * old_client_name,
87 const char * new_client_name)
89 struct list_head * node_ptr;
90 struct monitor * monitor_ptr;
92 list_for_each(node_ptr, &graph_ptr->monitors)
94 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
95 if (monitor_ptr->client_renamed != NULL)
97 monitor_ptr->client_renamed(monitor_ptr->context, client_id, old_client_name, new_client_name);
102 static void client_disappeared(struct graph * graph_ptr, uint64_t id)
104 struct list_head * node_ptr;
105 struct monitor * monitor_ptr;
107 list_for_each(node_ptr, &graph_ptr->monitors)
109 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
110 monitor_ptr->client_disappeared(monitor_ptr->context, id);
114 static
115 void
116 port_appeared(
117 struct graph * graph_ptr,
118 uint64_t client_id,
119 uint64_t port_id,
120 const char * port_name,
121 uint32_t port_flags,
122 uint32_t port_type)
124 struct list_head * node_ptr;
125 struct monitor * monitor_ptr;
126 bool is_input;
127 bool is_terminal;
128 bool is_midi;
130 if (port_type != JACKDBUS_PORT_TYPE_AUDIO && port_type != JACKDBUS_PORT_TYPE_MIDI)
132 log_error("Unknown JACK D-Bus port type %d", (unsigned int)port_type);
133 return;
136 is_input = port_flags & JACKDBUS_PORT_FLAG_INPUT;
137 is_terminal = port_flags & JACKDBUS_PORT_FLAG_TERMINAL;
138 is_midi = port_type == JACKDBUS_PORT_TYPE_MIDI;
140 list_for_each(node_ptr, &graph_ptr->monitors)
142 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
143 monitor_ptr->port_appeared(monitor_ptr->context, client_id, port_id, port_name, is_input, is_terminal, is_midi);
147 static
148 void
149 port_disappeared(
150 struct graph * graph_ptr,
151 uint64_t client_id,
152 uint64_t port_id)
154 struct list_head * node_ptr;
155 struct monitor * monitor_ptr;
157 list_for_each(node_ptr, &graph_ptr->monitors)
159 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
160 monitor_ptr->port_disappeared(monitor_ptr->context, client_id, port_id);
164 static
165 void
166 port_renamed(
167 struct graph * graph_ptr,
168 uint64_t client_id,
169 uint64_t port_id,
170 const char * old_port_name,
171 const char * new_port_name)
173 struct list_head * node_ptr;
174 struct monitor * monitor_ptr;
176 list_for_each(node_ptr, &graph_ptr->monitors)
178 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
179 monitor_ptr->port_renamed(monitor_ptr->context, client_id, port_id, old_port_name, new_port_name);
183 static
184 void
185 ports_connected(
186 struct graph * graph_ptr,
187 uint64_t client1_id,
188 uint64_t port1_id,
189 uint64_t client2_id,
190 uint64_t port2_id)
192 struct list_head * node_ptr;
193 struct monitor * monitor_ptr;
195 list_for_each(node_ptr, &graph_ptr->monitors)
197 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
198 monitor_ptr->ports_connected(monitor_ptr->context, client1_id, port1_id, client2_id, port2_id);
202 static
203 void
204 ports_disconnected(
205 struct graph * graph_ptr,
206 uint64_t client1_id,
207 uint64_t port1_id,
208 uint64_t client2_id,
209 uint64_t port2_id)
211 struct list_head * node_ptr;
212 struct monitor * monitor_ptr;
214 list_for_each(node_ptr, &graph_ptr->monitors)
216 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
217 monitor_ptr->ports_disconnected(monitor_ptr->context, client1_id, port1_id, client2_id, port2_id);
221 static void refresh_internal(struct graph * graph_ptr, bool force)
223 DBusMessage* reply_ptr;
224 DBusMessageIter iter;
225 dbus_uint64_t version;
226 const char * reply_signature;
227 DBusMessageIter clients_array_iter;
228 DBusMessageIter client_struct_iter;
229 DBusMessageIter ports_array_iter;
230 DBusMessageIter port_struct_iter;
231 DBusMessageIter connections_array_iter;
232 DBusMessageIter connection_struct_iter;
233 dbus_uint64_t client_id;
234 const char *client_name;
235 dbus_uint64_t port_id;
236 const char *port_name;
237 dbus_uint32_t port_flags;
238 dbus_uint32_t port_type;
239 dbus_uint64_t client2_id;
240 const char *client2_name;
241 dbus_uint64_t port2_id;
242 const char *port2_name;
243 dbus_uint64_t connection_id;
245 log_info("refresh_internal() called");
247 if (force)
249 version = 0; // workaround module split/join stupidity
251 else
253 version = graph_ptr->version;
256 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "GetGraph", "t", &version, NULL, &reply_ptr))
258 log_error("GetGraph() failed.");
259 return;
262 reply_signature = dbus_message_get_signature(reply_ptr);
264 if (strcmp(reply_signature, "ta(tsa(tsuu))a(tstststst)") != 0)
266 log_error("GetGraph() reply signature mismatch. '%s'", reply_signature);
267 goto unref;
270 dbus_message_iter_init(reply_ptr, &iter);
272 //log_info_msg("version " + (char)dbus_message_iter_get_arg_type(&iter));
273 dbus_message_iter_get_basic(&iter, &version);
274 dbus_message_iter_next(&iter);
276 if (!force && version <= graph_ptr->version)
278 goto unref;
281 clear(graph_ptr);
283 //log_info("got new graph version %llu", (unsigned long long)version);
284 graph_ptr->version = version;
286 //info_msg((std::string)"clients " + (char)dbus_message_iter_get_arg_type(&iter));
288 for (dbus_message_iter_recurse(&iter, &clients_array_iter);
289 dbus_message_iter_get_arg_type(&clients_array_iter) != DBUS_TYPE_INVALID;
290 dbus_message_iter_next(&clients_array_iter))
292 //info_msg((std::string)"a client " + (char)dbus_message_iter_get_arg_type(&clients_array_iter));
293 dbus_message_iter_recurse(&clients_array_iter, &client_struct_iter);
295 dbus_message_iter_get_basic(&client_struct_iter, &client_id);
296 dbus_message_iter_next(&client_struct_iter);
298 dbus_message_iter_get_basic(&client_struct_iter, &client_name);
299 dbus_message_iter_next(&client_struct_iter);
301 //info_msg((std::string)"client '" + client_name + "'");
303 client_appeared(graph_ptr, client_id, client_name);
305 for (dbus_message_iter_recurse(&client_struct_iter, &ports_array_iter);
306 dbus_message_iter_get_arg_type(&ports_array_iter) != DBUS_TYPE_INVALID;
307 dbus_message_iter_next(&ports_array_iter))
309 //info_msg((std::string)"a port " + (char)dbus_message_iter_get_arg_type(&ports_array_iter));
310 dbus_message_iter_recurse(&ports_array_iter, &port_struct_iter);
312 dbus_message_iter_get_basic(&port_struct_iter, &port_id);
313 dbus_message_iter_next(&port_struct_iter);
315 dbus_message_iter_get_basic(&port_struct_iter, &port_name);
316 dbus_message_iter_next(&port_struct_iter);
318 dbus_message_iter_get_basic(&port_struct_iter, &port_flags);
319 dbus_message_iter_next(&port_struct_iter);
321 dbus_message_iter_get_basic(&port_struct_iter, &port_type);
322 dbus_message_iter_next(&port_struct_iter);
324 //info_msg((std::string)"port: " + port_name);
326 port_appeared(graph_ptr, client_id, port_id, port_name, port_flags, port_type);
329 dbus_message_iter_next(&client_struct_iter);
332 dbus_message_iter_next(&iter);
334 for (dbus_message_iter_recurse(&iter, &connections_array_iter);
335 dbus_message_iter_get_arg_type(&connections_array_iter) != DBUS_TYPE_INVALID;
336 dbus_message_iter_next(&connections_array_iter))
338 //info_msg((std::string)"a connection " + (char)dbus_message_iter_get_arg_type(&connections_array_iter));
339 dbus_message_iter_recurse(&connections_array_iter, &connection_struct_iter);
341 dbus_message_iter_get_basic(&connection_struct_iter, &client_id);
342 dbus_message_iter_next(&connection_struct_iter);
344 dbus_message_iter_get_basic(&connection_struct_iter, &client_name);
345 dbus_message_iter_next(&connection_struct_iter);
347 dbus_message_iter_get_basic(&connection_struct_iter, &port_id);
348 dbus_message_iter_next(&connection_struct_iter);
350 dbus_message_iter_get_basic(&connection_struct_iter, &port_name);
351 dbus_message_iter_next(&connection_struct_iter);
353 dbus_message_iter_get_basic(&connection_struct_iter, &client2_id);
354 dbus_message_iter_next(&connection_struct_iter);
356 dbus_message_iter_get_basic(&connection_struct_iter, &client2_name);
357 dbus_message_iter_next(&connection_struct_iter);
359 dbus_message_iter_get_basic(&connection_struct_iter, &port2_id);
360 dbus_message_iter_next(&connection_struct_iter);
362 dbus_message_iter_get_basic(&connection_struct_iter, &port2_name);
363 dbus_message_iter_next(&connection_struct_iter);
365 dbus_message_iter_get_basic(&connection_struct_iter, &connection_id);
366 dbus_message_iter_next(&connection_struct_iter);
368 //info_msg(str(boost::format("connection(%llu) %s(%llu):%s(%llu) <-> %s(%llu):%s(%llu)") %
369 // connection_id %
370 // client_name %
371 // client_id %
372 // port_name %
373 // port_id %
374 // client2_name %
375 // client2_id %
376 // port2_name %
377 // port2_id));
379 ports_connected(graph_ptr, client_id, port_id, client2_id, port2_id);
382 unref:
383 dbus_message_unref(reply_ptr);
386 bool
387 graph_proxy_create(
388 const char * service,
389 const char * object,
390 bool graph_dict_supported,
391 bool graph_manager_supported,
392 graph_proxy_handle * graph_proxy_handle_ptr)
394 struct graph * graph_ptr;
396 graph_ptr = malloc(sizeof(struct graph));
397 if (graph_ptr == NULL)
399 log_error("malloc() failed to allocate struct graph");
400 goto fail;
403 graph_ptr->service = strdup(service);
404 if (graph_ptr->service == NULL)
406 log_error("strdup() failed too duplicate service name '%s'", service);
407 goto free_graph;
410 graph_ptr->object = strdup(object);
411 if (graph_ptr->object == NULL)
413 log_error("strdup() failed too duplicate object name '%s'", object);
414 goto free_service;
417 INIT_LIST_HEAD(&graph_ptr->monitors);
419 graph_ptr->version = 0;
420 graph_ptr->active = false;
422 graph_ptr->graph_dict_supported = graph_dict_supported;
423 graph_ptr->graph_manager_supported = graph_manager_supported;
425 *graph_proxy_handle_ptr = (graph_proxy_handle)graph_ptr;
427 return true;
429 free_service:
430 free(graph_ptr->service);
432 free_graph:
433 free(graph_ptr);
435 fail:
436 return false;
439 #define graph_ptr ((struct graph *)graph)
441 const char * graph_proxy_get_service(graph_proxy_handle graph)
443 return graph_ptr->service;
446 const char * graph_proxy_get_object(graph_proxy_handle graph)
448 return graph_ptr->object;
451 void
452 graph_proxy_destroy(
453 graph_proxy_handle graph)
455 ASSERT(list_empty(&graph_ptr->monitors));
457 if (graph_ptr->active)
459 cdbus_unregister_object_signal_hooks(
460 cdbus_g_dbus_connection,
461 graph_ptr->service,
462 graph_ptr->object,
463 JACKDBUS_IFACE_PATCHBAY);
466 free(graph_ptr->object);
467 free(graph_ptr->service);
468 free(graph_ptr);
471 bool
472 graph_proxy_activate(
473 graph_proxy_handle graph)
475 if (list_empty(&graph_ptr->monitors))
477 log_error("no monitors to activate");
478 return false;
481 if (graph_ptr->active)
483 log_error("graph already active");
484 return false;
487 if (!cdbus_register_object_signal_hooks(
488 cdbus_g_dbus_connection,
489 graph_ptr->service,
490 graph_ptr->object,
491 JACKDBUS_IFACE_PATCHBAY,
492 graph_ptr,
493 g_signal_hooks))
495 return false;
498 graph_ptr->active = true;
500 refresh_internal(graph_ptr, true);
502 return true;
505 bool
506 graph_proxy_attach(
507 graph_proxy_handle graph,
508 void * context,
509 void (* clear)(void * context),
510 void (* client_appeared)(void * context, uint64_t id, const char * name),
511 void (* client_renamed)(void * context, uint64_t client_id, const char * old_client_name, const char * new_client_name),
512 void (* client_disappeared)(void * context, uint64_t id),
513 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),
514 void (* port_renamed)(void * context, uint64_t client_id, uint64_t port_id, const char * old_port_name, const char * new_port_name),
515 void (* port_disappeared)(void * context, uint64_t client_id, uint64_t port_id),
516 void (* ports_connected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id),
517 void (* ports_disconnected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id))
519 struct monitor * monitor_ptr;
521 if (graph_ptr->active)
523 return false;
526 monitor_ptr = malloc(sizeof(struct monitor));
527 if (monitor_ptr == NULL)
529 log_error("malloc() failed to allocate struct monitor");
530 return false;
533 monitor_ptr->context = context;
534 monitor_ptr->clear = clear;
535 monitor_ptr->client_appeared = client_appeared;
536 monitor_ptr->client_renamed = client_renamed;
537 monitor_ptr->client_disappeared = client_disappeared;
538 monitor_ptr->port_appeared = port_appeared;
539 monitor_ptr->port_renamed = port_renamed;
540 monitor_ptr->port_disappeared = port_disappeared;
541 monitor_ptr->ports_connected = ports_connected;
542 monitor_ptr->ports_disconnected = ports_disconnected;
544 list_add_tail(&monitor_ptr->siblings, &graph_ptr->monitors);
546 return true;
549 void
550 graph_proxy_detach(
551 graph_proxy_handle graph,
552 void * context)
554 struct list_head * node_ptr;
555 struct monitor * monitor_ptr;
557 list_for_each(node_ptr, &graph_ptr->monitors)
559 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
560 if (monitor_ptr->context == context)
562 list_del(&monitor_ptr->siblings);
563 free(monitor_ptr);
564 return;
568 ASSERT(false);
571 bool
572 graph_proxy_connect_ports(
573 graph_proxy_handle graph,
574 uint64_t port1_id,
575 uint64_t port2_id)
577 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "ConnectPortsByID", "tt", &port1_id, &port2_id, ""))
579 log_error("ConnectPortsByID() failed.");
580 return false;
583 return true;
586 bool
587 graph_proxy_disconnect_ports(
588 graph_proxy_handle graph,
589 uint64_t port1_id,
590 uint64_t port2_id)
592 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "DisconnectPortsByID", "tt", &port1_id, &port2_id, ""))
594 log_error("DisconnectPortsByID() failed.");
595 return false;
598 return true;
601 static void on_client_appeared(void * graph, DBusMessage * message_ptr)
603 dbus_uint64_t new_graph_version;
604 dbus_uint64_t client_id;
605 const char * client_name;
607 if (!dbus_message_get_args(
608 message_ptr,
609 &cdbus_g_dbus_error,
610 DBUS_TYPE_UINT64, &new_graph_version,
611 DBUS_TYPE_UINT64, &client_id,
612 DBUS_TYPE_STRING, &client_name,
613 DBUS_TYPE_INVALID))
615 log_error("dbus_message_get_args() failed to extract ClientAppeared signal arguments (%s)", cdbus_g_dbus_error.message);
616 dbus_error_free(&cdbus_g_dbus_error);
617 return;
620 //log_info("ClientAppeared, %s(%llu), graph %llu", client_name, client_id, new_graph_version);
622 if (new_graph_version > graph_ptr->version)
624 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
625 graph_ptr->version = new_graph_version;
626 client_appeared(graph_ptr, client_id, client_name);
630 static void on_client_renamed(void * graph, DBusMessage * message_ptr)
632 dbus_uint64_t new_graph_version;
633 dbus_uint64_t client_id;
634 const char * old_client_name;
635 const char * new_client_name;
637 if (!dbus_message_get_args(
638 message_ptr,
639 &cdbus_g_dbus_error,
640 DBUS_TYPE_UINT64, &new_graph_version,
641 DBUS_TYPE_UINT64, &client_id,
642 DBUS_TYPE_STRING, &old_client_name,
643 DBUS_TYPE_STRING, &new_client_name,
644 DBUS_TYPE_INVALID))
646 log_error("dbus_message_get_args() failed to extract ClientRenamed signal arguments (%s)", cdbus_g_dbus_error.message);
647 dbus_error_free(&cdbus_g_dbus_error);
648 return;
651 if (new_graph_version > graph_ptr->version)
653 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
654 graph_ptr->version = new_graph_version;
655 client_renamed(graph_ptr, client_id, old_client_name, new_client_name);
659 static void on_client_disappeared(void * graph, DBusMessage * message_ptr)
661 dbus_uint64_t new_graph_version;
662 dbus_uint64_t client_id;
663 const char * client_name;
665 if (!dbus_message_get_args(
666 message_ptr,
667 &cdbus_g_dbus_error,
668 DBUS_TYPE_UINT64, &new_graph_version,
669 DBUS_TYPE_UINT64, &client_id,
670 DBUS_TYPE_STRING, &client_name,
671 DBUS_TYPE_INVALID))
673 log_error("dbus_message_get_args() failed to extract ClientDisappeared signal arguments (%s)", cdbus_g_dbus_error.message);
674 dbus_error_free(&cdbus_g_dbus_error);
675 return;
678 //log_info("ClientDisappeared, %s(%llu)", client_name, client_id);
680 if (new_graph_version > graph_ptr->version)
682 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
683 graph_ptr->version = new_graph_version;
684 client_disappeared(graph_ptr, client_id);
688 static void on_port_appeared(void * graph, DBusMessage * message_ptr)
690 dbus_uint64_t new_graph_version;
691 dbus_uint64_t client_id;
692 const char * client_name;
693 dbus_uint64_t port_id;
694 const char * port_name;
695 dbus_uint32_t port_flags;
696 dbus_uint32_t port_type;
698 if (!dbus_message_get_args(
699 message_ptr,
700 &cdbus_g_dbus_error,
701 DBUS_TYPE_UINT64, &new_graph_version,
702 DBUS_TYPE_UINT64, &client_id,
703 DBUS_TYPE_STRING, &client_name,
704 DBUS_TYPE_UINT64, &port_id,
705 DBUS_TYPE_STRING, &port_name,
706 DBUS_TYPE_UINT32, &port_flags,
707 DBUS_TYPE_UINT32, &port_type,
708 DBUS_TYPE_INVALID))
710 log_error("dbus_message_get_args() failed to extract PortAppeared signal arguments (%s)", cdbus_g_dbus_error.message);
711 dbus_error_free(&cdbus_g_dbus_error);
712 return;
715 //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));
717 if (new_graph_version > graph_ptr->version)
719 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
720 graph_ptr->version = new_graph_version;
721 port_appeared(graph_ptr, client_id, port_id, port_name, port_flags, port_type);
725 static void on_port_renamed(void * graph, DBusMessage * message_ptr)
727 dbus_uint64_t new_graph_version;
728 dbus_uint64_t client_id;
729 const char * client_name;
730 dbus_uint64_t port_id;
731 const char * old_port_name;
732 const char * new_port_name;
734 if (!dbus_message_get_args(
735 message_ptr,
736 &cdbus_g_dbus_error,
737 DBUS_TYPE_UINT64, &new_graph_version,
738 DBUS_TYPE_UINT64, &client_id,
739 DBUS_TYPE_STRING, &client_name,
740 DBUS_TYPE_UINT64, &port_id,
741 DBUS_TYPE_STRING, &old_port_name,
742 DBUS_TYPE_STRING, &new_port_name,
743 DBUS_TYPE_INVALID))
745 log_error("dbus_message_get_args() failed to extract PortRenamed signal arguments (%s)", cdbus_g_dbus_error.message);
746 dbus_error_free(&cdbus_g_dbus_error);
747 return;
750 if (new_graph_version > graph_ptr->version)
752 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
753 graph_ptr->version = new_graph_version;
754 port_renamed(graph_ptr, client_id, port_id, old_port_name, new_port_name);
758 static void on_port_disappeared(void * graph, DBusMessage * message_ptr)
760 dbus_uint64_t new_graph_version;
761 dbus_uint64_t client_id;
762 const char * client_name;
763 dbus_uint64_t port_id;
764 const char * port_name;
766 if (!dbus_message_get_args(
767 message_ptr,
768 &cdbus_g_dbus_error,
769 DBUS_TYPE_UINT64, &new_graph_version,
770 DBUS_TYPE_UINT64, &client_id,
771 DBUS_TYPE_STRING, &client_name,
772 DBUS_TYPE_UINT64, &port_id,
773 DBUS_TYPE_STRING, &port_name,
774 DBUS_TYPE_INVALID))
776 log_error("dbus_message_get_args() failed to extract PortDisappeared signal arguments (%s)", cdbus_g_dbus_error.message);
777 dbus_error_free(&cdbus_g_dbus_error);
778 return;
781 //me->info_msg(str(boost::format("PortDisappeared, %s(%llu):%s(%llu)") % client_name % client_id % port_name % port_id));
783 if (new_graph_version > graph_ptr->version)
785 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
786 graph_ptr->version = new_graph_version;
787 port_disappeared(graph_ptr, client_id, port_id);
791 static void on_ports_connected(void * graph, DBusMessage * message_ptr)
793 dbus_uint64_t new_graph_version;
794 dbus_uint64_t client_id;
795 const char * client_name;
796 dbus_uint64_t port_id;
797 const char * port_name;
798 dbus_uint64_t client2_id;
799 const char * client2_name;
800 dbus_uint64_t port2_id;
801 const char * port2_name;
802 dbus_uint64_t connection_id;
804 if (!dbus_message_get_args(
805 message_ptr,
806 &cdbus_g_dbus_error,
807 DBUS_TYPE_UINT64, &new_graph_version,
808 DBUS_TYPE_UINT64, &client_id,
809 DBUS_TYPE_STRING, &client_name,
810 DBUS_TYPE_UINT64, &port_id,
811 DBUS_TYPE_STRING, &port_name,
812 DBUS_TYPE_UINT64, &client2_id,
813 DBUS_TYPE_STRING, &client2_name,
814 DBUS_TYPE_UINT64, &port2_id,
815 DBUS_TYPE_STRING, &port2_name,
816 DBUS_TYPE_UINT64, &connection_id,
817 DBUS_TYPE_INVALID))
819 log_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", cdbus_g_dbus_error.message);
820 dbus_error_free(&cdbus_g_dbus_error);
821 return;
824 if (new_graph_version > graph_ptr->version)
826 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
827 graph_ptr->version = new_graph_version;
828 ports_connected(graph_ptr, client_id, port_id, client2_id, port2_id);
832 static void on_ports_disconnected(void * graph, DBusMessage * message_ptr)
834 dbus_uint64_t new_graph_version;
835 dbus_uint64_t client_id;
836 const char * client_name;
837 dbus_uint64_t port_id;
838 const char * port_name;
839 dbus_uint64_t client2_id;
840 const char * client2_name;
841 dbus_uint64_t port2_id;
842 const char * port2_name;
843 dbus_uint64_t connection_id;
845 if (!dbus_message_get_args(
846 message_ptr,
847 &cdbus_g_dbus_error,
848 DBUS_TYPE_UINT64, &new_graph_version,
849 DBUS_TYPE_UINT64, &client_id,
850 DBUS_TYPE_STRING, &client_name,
851 DBUS_TYPE_UINT64, &port_id,
852 DBUS_TYPE_STRING, &port_name,
853 DBUS_TYPE_UINT64, &client2_id,
854 DBUS_TYPE_STRING, &client2_name,
855 DBUS_TYPE_UINT64, &port2_id,
856 DBUS_TYPE_STRING, &port2_name,
857 DBUS_TYPE_UINT64, &connection_id,
858 DBUS_TYPE_INVALID))
860 log_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", cdbus_g_dbus_error.message);
861 dbus_error_free(&cdbus_g_dbus_error);
862 return;
865 if (new_graph_version > graph_ptr->version)
867 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
868 graph_ptr->version = new_graph_version;
869 ports_disconnected(graph_ptr, client_id, port_id, client2_id, port2_id);
873 bool
874 graph_proxy_dict_entry_set(
875 graph_proxy_handle graph,
876 uint32_t object_type,
877 uint64_t object_id,
878 const char * key,
879 const char * value)
881 if (!graph_ptr->graph_dict_supported)
883 return false;
886 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_DICT, "Set", "utss", &object_type, &object_id, &key, &value, ""))
888 log_error(IFACE_GRAPH_DICT ".Set() failed.");
889 return false;
892 return true;
895 bool
896 graph_proxy_dict_entry_get(
897 graph_proxy_handle graph,
898 uint32_t object_type,
899 uint64_t object_id,
900 const char * key,
901 char ** value_ptr_ptr)
903 DBusMessage * reply_ptr;
904 const char * reply_signature;
905 DBusMessageIter iter;
906 const char * cvalue_ptr;
907 char * value_ptr;
909 if (!graph_ptr->graph_dict_supported)
911 return false;
914 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_DICT, "Get", "uts", &object_type, &object_id, &key, NULL, &reply_ptr))
916 log_error(IFACE_GRAPH_DICT ".Get() failed.");
917 return false;
920 reply_signature = dbus_message_get_signature(reply_ptr);
922 if (strcmp(reply_signature, "s") != 0)
924 log_error("reply signature is '%s' but expected signature is 's'", reply_signature);
925 dbus_message_unref(reply_ptr);
926 return false;
929 dbus_message_iter_init(reply_ptr, &iter);
930 dbus_message_iter_get_basic(&iter, &cvalue_ptr);
931 value_ptr = strdup(cvalue_ptr);
932 dbus_message_unref(reply_ptr);
933 if (value_ptr == NULL)
935 log_error("strdup() failed for dict value");
936 return false;
938 *value_ptr_ptr = value_ptr;
939 return true;
942 bool
943 graph_proxy_dict_entry_drop(
944 graph_proxy_handle graph,
945 uint32_t object_type,
946 uint64_t object_id,
947 const char * key)
949 if (!graph_ptr->graph_dict_supported)
951 return false;
954 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_DICT, "Drop", "uts", &object_type, &object_id, &key, ""))
956 log_error(IFACE_GRAPH_DICT ".Drop() failed.");
957 return false;
960 return true;
963 bool graph_proxy_get_client_pid(graph_proxy_handle graph, uint64_t client_id, pid_t * pid_ptr)
965 int64_t pid;
967 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "GetClientPID", "t", &client_id, "x", &pid))
969 log_error("GetClientPID() failed.");
970 return false;
973 *pid_ptr = pid;
975 return true;
978 bool
979 graph_proxy_split(
980 graph_proxy_handle graph,
981 uint64_t client_id)
983 if (!graph_ptr->graph_manager_supported)
985 return false;
988 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_MANAGER, "Split", "t", &client_id, ""))
990 log_error(IFACE_GRAPH_MANAGER ".Split() failed.");
991 return false;
994 return true;
997 bool
998 graph_proxy_join(
999 graph_proxy_handle graph,
1000 uint64_t client1_id,
1001 uint64_t client2_id)
1003 if (!graph_ptr->graph_manager_supported)
1005 return false;
1008 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_MANAGER, "Join", "tt", &client1_id, &client2_id, ""))
1010 log_error(IFACE_GRAPH_MANAGER ".Join() failed.");
1011 return false;
1014 return true;
1017 bool
1018 graph_proxy_rename_client(
1019 graph_proxy_handle graph,
1020 uint64_t client_id,
1021 const char * newname)
1023 if (!graph_ptr->graph_manager_supported)
1025 return false;
1028 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_MANAGER, "RenameClient", "ts", &client_id, &newname, ""))
1030 log_error(IFACE_GRAPH_MANAGER ".RenameClient() failed.");
1031 return false;
1034 return true;
1037 bool
1038 graph_proxy_rename_port(
1039 graph_proxy_handle graph,
1040 uint64_t port_id,
1041 const char * newname)
1043 if (!graph_ptr->graph_manager_supported)
1045 return false;
1048 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_MANAGER, "RenamePort", "ts", &port_id, &newname, ""))
1050 log_error(IFACE_GRAPH_MANAGER ".RenamePort() failed.");
1051 return false;
1054 return true;
1057 bool
1058 graph_proxy_move_port(
1059 graph_proxy_handle graph,
1060 uint64_t port_id,
1061 uint64_t client_id)
1063 if (!graph_ptr->graph_manager_supported)
1065 return false;
1068 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_MANAGER, "MovePort", "tt", &port_id, &client_id, ""))
1070 log_error(IFACE_GRAPH_MANAGER ".MovePort() failed.");
1071 return false;
1074 return true;
1077 bool
1078 graph_proxy_new_client(
1079 graph_proxy_handle graph,
1080 const char * name,
1081 uint64_t * client_id_ptr)
1083 if (!graph_ptr->graph_manager_supported)
1085 return false;
1088 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_MANAGER, "NewClient", "s", &name, "t", client_id_ptr))
1090 log_error(IFACE_GRAPH_MANAGER ".NewClient() failed.");
1091 return false;
1094 return true;
1097 bool
1098 graph_proxy_remove_client(
1099 graph_proxy_handle graph,
1100 uint64_t client_id)
1102 if (!graph_ptr->graph_manager_supported)
1104 return false;
1107 if (!cdbus_call(0, graph_ptr->service, graph_ptr->object, IFACE_GRAPH_MANAGER, "RemoveClient", "t", &client_id, ""))
1109 log_error(IFACE_GRAPH_MANAGER ".RemoveClient() failed.");
1110 return false;
1113 return true;
1116 /* this must be static because it is referenced by the
1117 * dbus helper layer when hooks are active */
1118 static struct cdbus_signal_hook g_signal_hooks[] =
1120 {"ClientAppeared", on_client_appeared},
1121 {"ClientRenamed", on_client_renamed},
1122 {"ClientDisappeared", on_client_disappeared},
1123 {"PortAppeared", on_port_appeared},
1124 {"PortRenamed", on_port_renamed},
1125 {"PortDisappeared", on_port_disappeared},
1126 {"PortsConnected", on_ports_connected},
1127 {"PortsDisconnected", on_ports_disconnected},
1128 {NULL, NULL}