daemon: Fix #33
[ladish.git] / graph_proxy.c
blobdfb8b5bcc5536e9cfe78e8cfb38b7f2bc3d5ea3e
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
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>
28 #include <stdlib.h>
30 #include "common.h"
31 #include "graph_proxy.h"
32 #include "common/klist.h"
33 #include "dbus/helpers.h"
34 #include "dbus_constants.h"
36 struct monitor
38 struct list_head siblings;
39 void * context;
40 void (* clear)(void * context);
41 void (* client_appeared)(void * context, uint64_t id, const char * name);
42 void (* client_disappeared)(void * context, uint64_t id);
43 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);
44 void (* port_disappeared)(void * context, uint64_t client_id, uint64_t port_id);
45 void (* ports_connected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id);
46 void (* ports_disconnected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id);
49 struct graph
51 struct list_head monitors;
52 char * service;
53 char * object;
54 uint64_t version;
55 bool active;
56 bool graph_dict_supported;
59 static DBusHandlerResult message_hook(DBusConnection *, DBusMessage *, void *);
61 static const char * g_signals[] =
63 "ClientAppeared",
64 "ClientDisappeared",
65 "PortAppeared",
66 "PortDisappeared",
67 "PortsConnected",
68 "PortsDisconnected",
69 NULL
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);
108 static
109 void
110 port_appeared(
111 struct graph * graph_ptr,
112 uint64_t client_id,
113 uint64_t port_id,
114 const char * port_name,
115 uint32_t port_flags,
116 uint32_t port_type)
118 struct list_head * node_ptr;
119 struct monitor * monitor_ptr;
120 bool is_input;
121 bool is_terminal;
122 bool is_midi;
124 if (port_type != JACKDBUS_PORT_TYPE_AUDIO && port_type != JACKDBUS_PORT_TYPE_MIDI)
126 log_error("Unknown JACK D-Bus port type %d", (unsigned int)port_type);
127 return;
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);
141 static
142 void
143 port_disappeared(
144 struct graph * graph_ptr,
145 uint64_t client_id,
146 uint64_t port_id)
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);
158 static
159 void
160 ports_connected(
161 struct graph * graph_ptr,
162 uint64_t client1_id,
163 uint64_t port1_id,
164 uint64_t client2_id,
165 uint64_t port2_id)
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);
177 static
178 void
179 ports_disconnected(
180 struct graph * graph_ptr,
181 uint64_t client1_id,
182 uint64_t port1_id,
183 uint64_t client2_id,
184 uint64_t port2_id)
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 log_info("refresh_internal() called");
222 if (force)
224 version = 0; // workaround module split/join stupidity
226 else
228 version = graph_ptr->version;
231 if (!dbus_call(graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "GetGraph", "t", &version, NULL, &reply_ptr))
233 log_error("GetGraph() failed.");
234 return;
237 reply_signature = dbus_message_get_signature(reply_ptr);
239 if (strcmp(reply_signature, "ta(tsa(tsuu))a(tstststst)") != 0)
241 log_error("GetGraph() reply signature mismatch. '%s'", reply_signature);
242 goto unref;
245 dbus_message_iter_init(reply_ptr, &iter);
247 //log_info_msg("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)
253 goto unref;
256 clear(graph_ptr);
258 //log_info("got new graph version %llu", (unsigned long long)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)") %
344 // connection_id %
345 // client_name %
346 // client_id %
347 // port_name %
348 // port_id %
349 // client2_name %
350 // client2_id %
351 // port2_name %
352 // port2_id));
354 ports_connected(graph_ptr, client_id, port_id, client2_id, port2_id);
357 unref:
358 dbus_message_unref(reply_ptr);
361 bool
362 graph_proxy_create(
363 const char * service,
364 const char * object,
365 bool graph_dict_supported,
366 graph_proxy_handle * graph_proxy_handle_ptr)
368 struct graph * graph_ptr;
370 graph_ptr = malloc(sizeof(struct graph));
371 if (graph_ptr == NULL)
373 log_error("malloc() failed to allocate struct graph");
374 goto fail;
377 graph_ptr->service = strdup(service);
378 if (graph_ptr->service == NULL)
380 log_error("strdup() failed too duplicate service name '%s'", service);
381 goto free_graph;
384 graph_ptr->object = strdup(object);
385 if (graph_ptr->object == NULL)
387 log_error("strdup() failed too duplicate object name '%s'", object);
388 goto free_service;
391 INIT_LIST_HEAD(&graph_ptr->monitors);
393 graph_ptr->version = 0;
394 graph_ptr->active = false;
396 graph_ptr->graph_dict_supported = graph_dict_supported;
398 *graph_proxy_handle_ptr = (graph_proxy_handle)graph_ptr;
400 return true;
402 free_service:
403 free(graph_ptr->service);
405 free_graph:
406 free(graph_ptr);
408 fail:
409 return false;
412 #define graph_ptr ((struct graph *)graph)
414 const char * graph_proxy_get_service(graph_proxy_handle graph)
416 return graph_ptr->service;
419 const char * graph_proxy_get_object(graph_proxy_handle graph)
421 return graph_ptr->object;
424 void
425 graph_proxy_destroy(
426 graph_proxy_handle graph)
428 ASSERT(list_empty(&graph_ptr->monitors));
430 if (graph_ptr->active)
432 dbus_unregister_object_signal_handler(
433 g_dbus_connection,
434 graph_ptr->service,
435 graph_ptr->object,
436 JACKDBUS_IFACE_PATCHBAY,
437 g_signals,
438 message_hook,
439 graph_ptr);
442 free(graph_ptr->object);
443 free(graph_ptr->service);
444 free(graph_ptr);
447 bool
448 graph_proxy_activate(
449 graph_proxy_handle graph)
451 if (list_empty(&graph_ptr->monitors))
453 log_error("no monitors to activate");
454 return false;
457 if (graph_ptr->active)
459 log_error("graph already active");
460 return false;
463 if (!dbus_register_object_signal_handler(
464 g_dbus_connection,
465 graph_ptr->service,
466 graph_ptr->object,
467 JACKDBUS_IFACE_PATCHBAY,
468 g_signals,
469 message_hook,
470 graph_ptr))
472 return false;
475 graph_ptr->active = true;
477 refresh_internal(graph_ptr, true);
479 return true;
482 bool
483 graph_proxy_attach(
484 graph_proxy_handle graph,
485 void * context,
486 void (* clear)(void * context),
487 void (* client_appeared)(void * context, uint64_t id, const char * name),
488 void (* client_disappeared)(void * context, uint64_t id),
489 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),
490 void (* port_disappeared)(void * context, uint64_t client_id, uint64_t port_id),
491 void (* ports_connected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id),
492 void (* ports_disconnected)(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id))
494 struct monitor * monitor_ptr;
496 if (graph_ptr->active)
498 return false;
501 monitor_ptr = malloc(sizeof(struct monitor));
502 if (monitor_ptr == NULL)
504 log_error("malloc() failed to allocate struct monitor");
505 return false;
508 monitor_ptr->context = context;
509 monitor_ptr->clear = clear;
510 monitor_ptr->client_appeared = client_appeared;
511 monitor_ptr->client_disappeared = client_disappeared;
512 monitor_ptr->port_appeared = port_appeared;
513 monitor_ptr->port_disappeared = port_disappeared;
514 monitor_ptr->ports_connected = ports_connected;
515 monitor_ptr->ports_disconnected = ports_disconnected;
517 list_add_tail(&monitor_ptr->siblings, &graph_ptr->monitors);
519 return true;
522 void
523 graph_proxy_detach(
524 graph_proxy_handle graph,
525 void * context)
527 struct list_head * node_ptr;
528 struct monitor * monitor_ptr;
530 list_for_each(node_ptr, &graph_ptr->monitors)
532 monitor_ptr = list_entry(node_ptr, struct monitor, siblings);
533 if (monitor_ptr->context == context)
535 list_del(&monitor_ptr->siblings);
536 free(monitor_ptr);
537 return;
541 ASSERT(false);
544 void
545 graph_proxy_connect_ports(
546 graph_proxy_handle graph,
547 uint64_t port1_id,
548 uint64_t port2_id)
550 if (!dbus_call(graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "ConnectPortsByID", "tt", &port1_id, &port2_id, ""))
552 log_error("ConnectPortsByID() failed.");
556 void
557 graph_proxy_disconnect_ports(
558 graph_proxy_handle graph,
559 uint64_t port1_id,
560 uint64_t port2_id)
562 if (!dbus_call(graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "DisconnectPortsByID", "tt", &port1_id, &port2_id, ""))
564 log_error("DisconnectPortsByID() failed.");
568 static
569 DBusHandlerResult
570 message_hook(
571 DBusConnection * connection,
572 DBusMessage * message,
573 void * graph)
575 const char * object_path;
576 dbus_uint64_t new_graph_version;
577 dbus_uint64_t client_id;
578 const char *client_name;
579 dbus_uint64_t port_id;
580 const char *port_name;
581 dbus_uint32_t port_flags;
582 dbus_uint32_t port_type;
583 dbus_uint64_t client2_id;
584 const char *client2_name;
585 dbus_uint64_t port2_id;
586 const char *port2_name;
587 dbus_uint64_t connection_id;
589 object_path = dbus_message_get_path(message);
590 if (object_path == NULL || strcmp(object_path, graph_ptr->object) != 0)
592 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
595 if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "ClientAppeared"))
597 if (!dbus_message_get_args(
598 message,
599 &g_dbus_error,
600 DBUS_TYPE_UINT64, &new_graph_version,
601 DBUS_TYPE_UINT64, &client_id,
602 DBUS_TYPE_STRING, &client_name,
603 DBUS_TYPE_INVALID))
605 log_error("dbus_message_get_args() failed to extract ClientAppeared signal arguments (%s)", g_dbus_error.message);
606 dbus_error_free(&g_dbus_error);
607 return DBUS_HANDLER_RESULT_HANDLED;
610 //log_info("ClientAppeared, %s(%llu), graph %llu", client_name, client_id, new_graph_version);
612 if (new_graph_version > graph_ptr->version)
614 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
615 graph_ptr->version = new_graph_version;
616 client_appeared(graph_ptr, client_id, client_name);
619 return DBUS_HANDLER_RESULT_HANDLED;
622 if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "ClientDisappeared"))
624 if (!dbus_message_get_args(
625 message,
626 &g_dbus_error,
627 DBUS_TYPE_UINT64, &new_graph_version,
628 DBUS_TYPE_UINT64, &client_id,
629 DBUS_TYPE_STRING, &client_name,
630 DBUS_TYPE_INVALID))
632 log_error("dbus_message_get_args() failed to extract ClientDisappeared signal arguments (%s)", g_dbus_error.message);
633 dbus_error_free(&g_dbus_error);
634 return DBUS_HANDLER_RESULT_HANDLED;
637 //log_info("ClientDisappeared, %s(%llu)", client_name, client_id);
639 if (new_graph_version > graph_ptr->version)
641 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
642 graph_ptr->version = new_graph_version;
643 client_disappeared(graph_ptr, client_id);
646 return DBUS_HANDLER_RESULT_HANDLED;
649 if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortAppeared"))
651 if (!dbus_message_get_args(
652 message,
653 &g_dbus_error,
654 DBUS_TYPE_UINT64, &new_graph_version,
655 DBUS_TYPE_UINT64, &client_id,
656 DBUS_TYPE_STRING, &client_name,
657 DBUS_TYPE_UINT64, &port_id,
658 DBUS_TYPE_STRING, &port_name,
659 DBUS_TYPE_UINT32, &port_flags,
660 DBUS_TYPE_UINT32, &port_type,
661 DBUS_TYPE_INVALID))
663 log_error("dbus_message_get_args() failed to extract PortAppeared signal arguments (%s)", g_dbus_error.message);
664 dbus_error_free(&g_dbus_error);
665 return DBUS_HANDLER_RESULT_HANDLED;
668 //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));
670 if (new_graph_version > graph_ptr->version)
672 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
673 graph_ptr->version = new_graph_version;
674 port_appeared(graph_ptr, client_id, port_id, port_name, port_flags, port_type);
677 return DBUS_HANDLER_RESULT_HANDLED;
680 if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortDisappeared"))
682 if (!dbus_message_get_args(
683 message,
684 &g_dbus_error,
685 DBUS_TYPE_UINT64, &new_graph_version,
686 DBUS_TYPE_UINT64, &client_id,
687 DBUS_TYPE_STRING, &client_name,
688 DBUS_TYPE_UINT64, &port_id,
689 DBUS_TYPE_STRING, &port_name,
690 DBUS_TYPE_INVALID))
692 log_error("dbus_message_get_args() failed to extract PortDisappeared signal arguments (%s)", g_dbus_error.message);
693 dbus_error_free(&g_dbus_error);
694 return DBUS_HANDLER_RESULT_HANDLED;
697 //me->info_msg(str(boost::format("PortDisappeared, %s(%llu):%s(%llu)") % client_name % client_id % port_name % port_id));
699 if (new_graph_version > graph_ptr->version)
701 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
702 graph_ptr->version = new_graph_version;
703 port_disappeared(graph_ptr, client_id, port_id);
706 return DBUS_HANDLER_RESULT_HANDLED;
709 if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortsConnected"))
711 if (!dbus_message_get_args(
712 message,
713 &g_dbus_error,
714 DBUS_TYPE_UINT64, &new_graph_version,
715 DBUS_TYPE_UINT64, &client_id,
716 DBUS_TYPE_STRING, &client_name,
717 DBUS_TYPE_UINT64, &port_id,
718 DBUS_TYPE_STRING, &port_name,
719 DBUS_TYPE_UINT64, &client2_id,
720 DBUS_TYPE_STRING, &client2_name,
721 DBUS_TYPE_UINT64, &port2_id,
722 DBUS_TYPE_STRING, &port2_name,
723 DBUS_TYPE_UINT64, &connection_id,
724 DBUS_TYPE_INVALID))
726 log_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", g_dbus_error.message);
727 dbus_error_free(&g_dbus_error);
728 return DBUS_HANDLER_RESULT_HANDLED;
731 if (new_graph_version > graph_ptr->version)
733 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
734 graph_ptr->version = new_graph_version;
735 ports_connected(graph_ptr, client_id, port_id, client2_id, port2_id);
738 return DBUS_HANDLER_RESULT_HANDLED;
741 if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortsDisconnected"))
743 if (!dbus_message_get_args(
744 message,
745 &g_dbus_error,
746 DBUS_TYPE_UINT64, &new_graph_version,
747 DBUS_TYPE_UINT64, &client_id,
748 DBUS_TYPE_STRING, &client_name,
749 DBUS_TYPE_UINT64, &port_id,
750 DBUS_TYPE_STRING, &port_name,
751 DBUS_TYPE_UINT64, &client2_id,
752 DBUS_TYPE_STRING, &client2_name,
753 DBUS_TYPE_UINT64, &port2_id,
754 DBUS_TYPE_STRING, &port2_name,
755 DBUS_TYPE_UINT64, &connection_id,
756 DBUS_TYPE_INVALID))
758 log_error("dbus_message_get_args() failed to extract PortsConnected signal arguments (%s)", g_dbus_error.message);
759 dbus_error_free(&g_dbus_error);
760 return DBUS_HANDLER_RESULT_HANDLED;
763 if (new_graph_version > graph_ptr->version)
765 //log_info("got new graph version %llu", (unsigned long long)new_graph_version);
766 graph_ptr->version = new_graph_version;
767 ports_disconnected(graph_ptr, client_id, port_id, client2_id, port2_id);
770 return DBUS_HANDLER_RESULT_HANDLED;
773 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
776 bool
777 graph_proxy_dict_entry_set(
778 graph_proxy_handle graph,
779 uint32_t object_type,
780 uint64_t object_id,
781 const char * key,
782 const char * value)
784 if (!graph_ptr->graph_dict_supported)
786 return false;
789 if (!dbus_call(graph_ptr->service, graph_ptr->object, IFACE_GRAPH_DICT, "Set", "utss", &object_type, &object_id, &key, &value, ""))
791 log_error(IFACE_GRAPH_DICT ".Set() failed.");
792 return false;
795 return true;
798 bool
799 graph_proxy_dict_entry_get(
800 graph_proxy_handle graph,
801 uint32_t object_type,
802 uint64_t object_id,
803 const char * key,
804 char ** value_ptr_ptr)
806 DBusMessage * reply_ptr;
807 const char * reply_signature;
808 DBusMessageIter iter;
809 const char * cvalue_ptr;
810 char * value_ptr;
812 if (!graph_ptr->graph_dict_supported)
814 return false;
817 if (!dbus_call(graph_ptr->service, graph_ptr->object, IFACE_GRAPH_DICT, "Get", "uts", &object_type, &object_id, &key, NULL, &reply_ptr))
819 log_error(IFACE_GRAPH_DICT ".Get() failed.");
820 return false;
823 reply_signature = dbus_message_get_signature(reply_ptr);
825 if (strcmp(reply_signature, "s") != 0)
827 log_error("reply signature is '%s' but expected signature is 's'", reply_signature);
828 dbus_message_unref(reply_ptr);
829 return false;
832 dbus_message_iter_init(reply_ptr, &iter);
833 dbus_message_iter_get_basic(&iter, &cvalue_ptr);
834 value_ptr = strdup(cvalue_ptr);
835 dbus_message_unref(reply_ptr);
836 if (value_ptr == NULL)
838 log_error("strdup() failed for dict value");
839 return false;
841 *value_ptr_ptr = value_ptr;
842 return true;
845 bool
846 graph_proxy_dict_entry_drop(
847 graph_proxy_handle graph,
848 uint32_t object_type,
849 uint64_t object_id,
850 const char * key)
852 if (!graph_ptr->graph_dict_supported)
854 return false;
857 if (!dbus_call(graph_ptr->service, graph_ptr->object, IFACE_GRAPH_DICT, "Drop", "uts", &object_type, &object_id, &key, ""))
859 log_error(IFACE_GRAPH_DICT ".Drop() failed.");
860 return false;
863 return true;
866 bool graph_proxy_get_client_pid(graph_proxy_handle graph, uint64_t client_id, int64_t * pid_ptr)
868 if (!dbus_call(graph_ptr->service, graph_ptr->object, JACKDBUS_IFACE_PATCHBAY, "GetClientPID", "t", &client_id, "x", pid_ptr))
870 log_error("GetClientPID() failed.");
871 return false;
874 return true;