daemon: add some logs related to hiding ports and connections
[ladish.git] / daemon / graph.c
blob1d2ee85e51f3ac5918f72045c5e9164889e55840
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari
8 **************************************************************************
9 * This file contains implementation of the D-Bus patchbay interface helpers
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "common.h"
29 #include "graph.h"
30 #include "../dbus/error.h"
31 #include "../dbus_constants.h"
33 struct ladish_graph_port
35 struct list_head siblings_client;
36 struct list_head siblings_graph;
37 struct ladish_graph_client * client_ptr;
38 char * name;
39 uint32_t type;
40 uint32_t flags;
41 uint64_t id;
42 ladish_port_handle port;
43 bool hidden;
46 struct ladish_graph_client
48 struct list_head siblings;
49 char * name;
50 uint64_t id;
51 ladish_client_handle client;
52 struct list_head ports;
53 bool hidden;
56 struct ladish_graph_connection
58 struct list_head siblings;
59 uint64_t id;
60 bool hidden;
61 struct ladish_graph_port * port1_ptr;
62 struct ladish_graph_port * port2_ptr;
63 ladish_dict_handle dict;
64 bool changing;
67 struct ladish_graph
69 char * opath;
70 ladish_dict_handle dict;
71 struct list_head clients;
72 struct list_head ports;
73 struct list_head connections;
74 uint64_t graph_version;
75 uint64_t next_client_id;
76 uint64_t next_port_id;
77 uint64_t next_connection_id;
79 void * context;
80 ladish_graph_connect_request_handler connect_handler;
81 ladish_graph_disconnect_request_handler disconnect_handler;
84 struct ladish_graph_port * ladish_graph_find_port_by_id_internal(struct ladish_graph * graph_ptr, uint64_t port_id)
86 struct list_head * node_ptr;
87 struct ladish_graph_port * port_ptr;
89 list_for_each(node_ptr, &graph_ptr->ports)
91 port_ptr = list_entry(node_ptr, struct ladish_graph_port, siblings_graph);
92 if (port_ptr->id == port_id)
94 return port_ptr;
98 return NULL;
101 struct ladish_graph_connection * ladish_graph_find_connection_by_id(struct ladish_graph * graph_ptr, uint64_t connection_id)
103 struct list_head * node_ptr;
104 struct ladish_graph_connection * connection_ptr;
106 list_for_each(node_ptr, &graph_ptr->connections)
108 connection_ptr = list_entry(node_ptr, struct ladish_graph_connection, siblings);
109 if (connection_ptr->id == connection_id)
111 return connection_ptr;
115 return NULL;
118 struct ladish_graph_connection *
119 ladish_graph_find_connection_by_ports(
120 struct ladish_graph * graph_ptr,
121 struct ladish_graph_port * port1_ptr,
122 struct ladish_graph_port * port2_ptr)
124 struct list_head * node_ptr;
125 struct ladish_graph_connection * connection_ptr;
127 list_for_each(node_ptr, &graph_ptr->connections)
129 connection_ptr = list_entry(node_ptr, struct ladish_graph_connection, siblings);
130 if ((connection_ptr->port1_ptr == port1_ptr && connection_ptr->port2_ptr == port2_ptr) ||
131 (connection_ptr->port1_ptr == port2_ptr && connection_ptr->port2_ptr == port1_ptr))
133 return connection_ptr;
137 return NULL;
140 #define graph_ptr ((struct ladish_graph *)call_ptr->iface_context)
142 static void get_all_ports(struct dbus_method_call * call_ptr)
144 DBusMessageIter iter, sub_iter;
146 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
147 if (call_ptr->reply == NULL)
149 goto fail;
152 dbus_message_iter_init_append(call_ptr->reply, &iter);
154 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
156 goto fail_unref;
159 if (!dbus_message_iter_close_container(&iter, &sub_iter))
161 goto fail_unref;
164 return;
166 fail_unref:
167 dbus_message_unref(call_ptr->reply);
168 call_ptr->reply = NULL;
170 fail:
171 log_error("Ran out of memory trying to construct method return");
174 static void get_graph(struct dbus_method_call * call_ptr)
176 dbus_uint64_t known_version;
177 dbus_uint64_t current_version;
178 DBusMessageIter iter;
179 DBusMessageIter clients_array_iter;
180 DBusMessageIter connections_array_iter;
181 DBusMessageIter client_struct_iter;
182 struct list_head * client_node_ptr;
183 struct ladish_graph_client * client_ptr;
184 DBusMessageIter ports_array_iter;
185 struct list_head * port_node_ptr;
186 struct ladish_graph_port * port_ptr;
187 DBusMessageIter port_struct_iter;
188 struct list_head * connection_node_ptr;
189 struct ladish_graph_connection * connection_ptr;
190 DBusMessageIter connection_struct_iter;
192 //log_info("get_graph() called");
194 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_UINT64, &known_version, DBUS_TYPE_INVALID))
196 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
197 dbus_error_free(&g_dbus_error);
198 return;
201 //log_info("Getting graph, known version is %" PRIu64, known_version);
203 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
204 if (call_ptr->reply == NULL)
206 log_error("Ran out of memory trying to construct method return");
207 goto exit;
210 dbus_message_iter_init_append(call_ptr->reply, &iter);
212 current_version = graph_ptr->graph_version;
213 if (known_version > current_version)
215 lash_dbus_error(
216 call_ptr,
217 LASH_DBUS_ERROR_INVALID_ARGS,
218 "known graph version %" PRIu64 " is newer than actual version %" PRIu64,
219 known_version,
220 current_version);
221 goto exit;
224 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &current_version))
226 goto nomem;
229 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tsa(tsuu))", &clients_array_iter))
231 goto nomem;
234 if (known_version < current_version)
236 list_for_each(client_node_ptr, &graph_ptr->clients)
238 client_ptr = list_entry(client_node_ptr, struct ladish_graph_client, siblings);
240 if (client_ptr->hidden)
242 continue;
245 if (!dbus_message_iter_open_container (&clients_array_iter, DBUS_TYPE_STRUCT, NULL, &client_struct_iter))
247 goto nomem_close_clients_array;
250 if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_UINT64, &client_ptr->id))
252 goto nomem_close_client_struct;
255 log_info("client '%s' (%llu)", client_ptr->name, (unsigned long long)client_ptr->id);
256 if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_STRING, &client_ptr->name))
258 goto nomem_close_client_struct;
261 if (!dbus_message_iter_open_container(&client_struct_iter, DBUS_TYPE_ARRAY, "(tsuu)", &ports_array_iter))
263 goto nomem_close_client_struct;
266 list_for_each(port_node_ptr, &client_ptr->ports)
268 port_ptr = list_entry(port_node_ptr, struct ladish_graph_port, siblings_client);
270 if (port_ptr->hidden)
272 continue;
275 if (!dbus_message_iter_open_container(&ports_array_iter, DBUS_TYPE_STRUCT, NULL, &port_struct_iter))
277 goto nomem_close_ports_array;
280 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT64, &port_ptr->id))
282 goto nomem_close_port_struct;
285 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_STRING, &port_ptr->name))
287 goto nomem_close_port_struct;
290 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->flags))
292 goto nomem_close_port_struct;
295 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->type))
297 goto nomem_close_port_struct;
300 if (!dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter))
302 goto nomem_close_ports_array;
306 if (!dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter))
308 goto nomem_close_client_struct;
311 if (!dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter))
313 goto nomem_close_clients_array;
318 if (!dbus_message_iter_close_container(&iter, &clients_array_iter))
320 goto nomem;
323 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tstststst)", &connections_array_iter))
325 goto nomem;
328 if (known_version < current_version)
330 list_for_each(connection_node_ptr, &graph_ptr->connections)
332 connection_ptr = list_entry(connection_node_ptr, struct ladish_graph_connection, siblings);
334 if (connection_ptr->hidden)
336 continue;
339 if (!dbus_message_iter_open_container(&connections_array_iter, DBUS_TYPE_STRUCT, NULL, &connection_struct_iter))
341 goto nomem_close_connections_array;
344 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1_ptr->client_ptr->id))
346 goto nomem_close_connection_struct;
349 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1_ptr->client_ptr->name))
351 goto nomem_close_connection_struct;
354 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1_ptr->id))
356 goto nomem_close_connection_struct;
359 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1_ptr->name))
361 goto nomem_close_connection_struct;
364 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2_ptr->client_ptr->id))
366 goto nomem_close_connection_struct;
369 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2_ptr->client_ptr->name))
371 goto nomem_close_connection_struct;
374 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2_ptr->id))
376 goto nomem_close_connection_struct;
379 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2_ptr->name))
381 goto nomem_close_connection_struct;
384 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->id))
386 goto nomem_close_connection_struct;
389 if (!dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter))
391 goto nomem_close_connections_array;
396 if (!dbus_message_iter_close_container(&iter, &connections_array_iter))
398 goto nomem;
401 return;
403 nomem_close_connection_struct:
404 dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter);
406 nomem_close_connections_array:
407 dbus_message_iter_close_container(&iter, &connections_array_iter);
408 goto nomem;
410 nomem_close_port_struct:
411 dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter);
413 nomem_close_ports_array:
414 dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter);
416 nomem_close_client_struct:
417 dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter);
419 nomem_close_clients_array:
420 dbus_message_iter_close_container(&iter, &clients_array_iter);
422 nomem:
423 dbus_message_unref(call_ptr->reply);
424 call_ptr->reply = NULL;
425 log_error("Ran out of memory trying to construct method return");
427 exit:
428 return;
431 static void connect_ports_by_name(struct dbus_method_call * call_ptr)
433 log_info("connect_ports_by_name() called.");
434 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "connect by name is not implemented yet");
437 static void connect_ports_by_id(struct dbus_method_call * call_ptr)
439 dbus_uint64_t port1_id;
440 dbus_uint64_t port2_id;
441 struct ladish_graph_port * port1_ptr;
442 struct ladish_graph_port * port2_ptr;
444 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_UINT64, &port1_id, DBUS_TYPE_UINT64, &port2_id, DBUS_TYPE_INVALID))
446 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
447 dbus_error_free(&g_dbus_error);
448 return;
451 log_info("connect_ports_by_id(%"PRIu64",%"PRIu64") called.", port1_id, port2_id);
453 if (graph_ptr->connect_handler == NULL)
455 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "connect requests on this graph cannot be handlined");
456 return;
459 port1_ptr = ladish_graph_find_port_by_id_internal(graph_ptr, port1_id);
460 if (port1_ptr == NULL)
462 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Cannot connect unknown port with id %"PRIu64, port1_id);
463 return;
466 port2_ptr = ladish_graph_find_port_by_id_internal(graph_ptr, port2_id);
467 if (port2_ptr == NULL)
469 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Cannot connect unknown port with id %"PRIu64, port2_id);
470 return;
473 log_info("connecting '%s':'%s' to '%s':'%s'", port1_ptr->client_ptr->name, port1_ptr->name, port2_ptr->client_ptr->name, port2_ptr->name);
475 if (graph_ptr->connect_handler(graph_ptr->context, (ladish_graph_handle)graph_ptr, port1_ptr->port, port2_ptr->port))
477 method_return_new_void(call_ptr);
479 else
481 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "connect failed");
485 static void disconnect_ports(struct dbus_method_call * call_ptr, struct ladish_graph_connection * connection_ptr)
487 log_info(
488 "disconnecting '%s':'%s' from '%s':'%s'",
489 connection_ptr->port1_ptr->client_ptr->name,
490 connection_ptr->port1_ptr->name,
491 connection_ptr->port2_ptr->client_ptr->name,
492 connection_ptr->port2_ptr->name);
494 connection_ptr->changing = true;
495 if (graph_ptr->disconnect_handler(graph_ptr->context, (ladish_graph_handle)graph_ptr, connection_ptr->id))
497 method_return_new_void(call_ptr);
499 else
501 connection_ptr->changing = false;
502 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "disconnect failed");
506 static void disconnect_ports_by_name(struct dbus_method_call * call_ptr)
508 log_info("disconnect_ports_by_name() called.");
509 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "disconnect by name is not implemented yet");
512 static void disconnect_ports_by_id(struct dbus_method_call * call_ptr)
514 dbus_uint64_t port1_id;
515 dbus_uint64_t port2_id;
516 struct ladish_graph_port * port1_ptr;
517 struct ladish_graph_port * port2_ptr;
518 struct ladish_graph_connection * connection_ptr;
520 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_UINT64, &port1_id, DBUS_TYPE_UINT64, &port2_id, DBUS_TYPE_INVALID))
522 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
523 dbus_error_free(&g_dbus_error);
524 return;
527 log_info("disconnect_ports_by_id(%"PRIu64",%"PRIu64") called.", port1_id, port2_id);
529 if (graph_ptr->disconnect_handler == NULL)
531 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "disconnect requests on this graph cannot be handlined");
532 return;
535 port1_ptr = ladish_graph_find_port_by_id_internal(graph_ptr, port1_id);
536 if (port1_ptr == NULL)
538 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Cannot disconnect unknown port with id %"PRIu64, port1_id);
539 return;
542 port2_ptr = ladish_graph_find_port_by_id_internal(graph_ptr, port2_id);
543 if (port2_ptr == NULL)
545 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Cannot disconnect unknown port with id %"PRIu64, port2_id);
546 return;
549 connection_ptr = ladish_graph_find_connection_by_ports(graph_ptr, port1_ptr, port2_ptr);
550 if (connection_ptr == NULL)
552 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Cannot disconnect not connected ports %"PRIu64" and %"PRIu64, port1_id, port2_id);
553 return;
556 disconnect_ports(call_ptr, connection_ptr);
559 static void disconnect_ports_by_connection_id(struct dbus_method_call * call_ptr)
561 dbus_uint64_t connection_id;
562 struct ladish_graph_connection * connection_ptr;
564 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_UINT64, &connection_id, DBUS_TYPE_INVALID))
566 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
567 dbus_error_free(&g_dbus_error);
568 return;
571 log_info("disconnect_ports_by_connection_id(%"PRIu64") called.", connection_id);
573 connection_ptr = ladish_graph_find_connection_by_id(graph_ptr, connection_id);
574 if (connection_ptr == NULL)
576 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Cannot find connection with id %"PRIu64, connection_id);
577 return;
580 disconnect_ports(call_ptr, connection_ptr);
583 static void get_client_pid(struct dbus_method_call * call_ptr)
585 int64_t pid = 0;
586 method_return_new_single(call_ptr, DBUS_TYPE_INT64, &pid);
589 #undef graph_ptr
591 bool ladish_graph_create(ladish_graph_handle * graph_handle_ptr, const char * opath)
593 struct ladish_graph * graph_ptr;
595 graph_ptr = malloc(sizeof(struct ladish_graph));
596 if (graph_ptr == NULL)
598 log_error("malloc() failed to allocate struct graph_implementator");
599 return false;
602 if (opath != NULL)
604 graph_ptr->opath = strdup(opath);
605 if (graph_ptr->opath == NULL)
607 log_error("strdup() failed for graph opath");
608 free(graph_ptr);
609 return false;
612 else
614 graph_ptr->opath = NULL;
617 if (!ladish_dict_create(&graph_ptr->dict))
619 log_error("ladish_dict_create() failed for graph");
620 if (graph_ptr->opath != NULL)
622 free(graph_ptr->opath);
624 free(graph_ptr);
625 return false;
628 INIT_LIST_HEAD(&graph_ptr->clients);
629 INIT_LIST_HEAD(&graph_ptr->ports);
630 INIT_LIST_HEAD(&graph_ptr->connections);
632 graph_ptr->graph_version = 1;
633 graph_ptr->next_client_id = 1;
634 graph_ptr->next_port_id = 1;
635 graph_ptr->next_connection_id = 1;
637 graph_ptr->context = NULL;
638 graph_ptr->connect_handler = NULL;
639 graph_ptr->disconnect_handler = NULL;
641 *graph_handle_ptr = (ladish_graph_handle)graph_ptr;
642 return true;
645 static
646 struct ladish_graph_client *
647 ladish_graph_find_client(
648 struct ladish_graph * graph_ptr,
649 ladish_client_handle client)
651 struct list_head * node_ptr;
652 struct ladish_graph_client * client_ptr;
654 list_for_each(node_ptr, &graph_ptr->clients)
656 client_ptr = list_entry(node_ptr, struct ladish_graph_client, siblings);
657 if (client_ptr->client == client)
659 return client_ptr;
663 return NULL;
666 static
667 struct ladish_graph_port *
668 ladish_graph_find_port(
669 struct ladish_graph * graph_ptr,
670 ladish_port_handle port)
672 struct list_head * node_ptr;
673 struct ladish_graph_port * port_ptr;
675 list_for_each(node_ptr, &graph_ptr->ports)
677 port_ptr = list_entry(node_ptr, struct ladish_graph_port, siblings_graph);
678 if (port_ptr->port == port)
680 return port_ptr;
684 return NULL;
687 #if 0
688 static
689 struct ladish_graph_port *
690 ladish_graph_find_client_port(
691 struct ladish_graph * graph_ptr,
692 struct ladish_graph_client * client_ptr,
693 ladish_port_handle port)
695 struct list_head * node_ptr;
696 struct ladish_graph_port * port_ptr;
698 list_for_each(node_ptr, &client_ptr->ports)
700 port_ptr = list_entry(node_ptr, struct ladish_graph_port, siblings_client);
701 if (port_ptr->port == port)
703 return port_ptr;
707 return NULL;
709 #endif
711 static void ladish_graph_hide_connection_internal(struct ladish_graph * graph_ptr, struct ladish_graph_connection * connection_ptr)
713 connection_ptr->hidden = true;
714 graph_ptr->graph_version++;
716 if (graph_ptr->opath != NULL)
718 dbus_signal_emit(
719 g_dbus_connection,
720 graph_ptr->opath,
721 JACKDBUS_IFACE_PATCHBAY,
722 "PortsDisconnected",
723 "ttstststst",
724 &graph_ptr->graph_version,
725 &connection_ptr->port1_ptr->client_ptr->id,
726 &connection_ptr->port1_ptr->client_ptr->name,
727 &connection_ptr->port1_ptr->id,
728 &connection_ptr->port1_ptr->name,
729 &connection_ptr->port2_ptr->client_ptr->id,
730 &connection_ptr->port2_ptr->client_ptr->name,
731 &connection_ptr->port2_ptr->id,
732 &connection_ptr->port2_ptr->name,
733 &connection_ptr->id);
737 void ladish_hide_connections(struct ladish_graph * graph_ptr, struct ladish_graph_port * port_ptr)
739 struct list_head * node_ptr;
740 struct ladish_graph_connection * connection_ptr;
742 log_info("hidding connections of port %"PRIu64, port_ptr->id);
744 ASSERT(graph_ptr->opath != NULL);
746 list_for_each(node_ptr, &graph_ptr->connections)
748 connection_ptr = list_entry(node_ptr, struct ladish_graph_connection, siblings);
749 if (!connection_ptr->hidden &&
750 (connection_ptr->port1_ptr == port_ptr || connection_ptr->port2_ptr == port_ptr) &&
751 (connection_ptr->port1_ptr->hidden || connection_ptr->port2_ptr->hidden))
753 log_info("hidding connection between ports %"PRIu64" and %"PRIu64, connection_ptr->port1_ptr->id, connection_ptr->port2_ptr->id);
754 ladish_graph_hide_connection_internal(graph_ptr, connection_ptr);
759 static void ladish_graph_remove_connection_internal(struct ladish_graph * graph_ptr, struct ladish_graph_connection * connection_ptr)
761 list_del(&connection_ptr->siblings);
762 graph_ptr->graph_version++;
764 if (!connection_ptr->hidden && graph_ptr->opath != NULL)
766 dbus_signal_emit(
767 g_dbus_connection,
768 graph_ptr->opath,
769 JACKDBUS_IFACE_PATCHBAY,
770 "PortsDisconnected",
771 "ttstststst",
772 &graph_ptr->graph_version,
773 &connection_ptr->port1_ptr->client_ptr->id,
774 &connection_ptr->port1_ptr->client_ptr->name,
775 &connection_ptr->port1_ptr->id,
776 &connection_ptr->port1_ptr->name,
777 &connection_ptr->port2_ptr->client_ptr->id,
778 &connection_ptr->port2_ptr->client_ptr->name,
779 &connection_ptr->port2_ptr->id,
780 &connection_ptr->port2_ptr->name,
781 &connection_ptr->id);
784 ladish_dict_destroy(connection_ptr->dict);
785 free(connection_ptr);
788 void
789 ladish_graph_remove_port_internal(
790 struct ladish_graph * graph_ptr,
791 struct ladish_graph_client * client_ptr,
792 struct ladish_graph_port * port_ptr,
793 bool destroy)
795 if (destroy)
797 ladish_port_destroy(port_ptr->port);
800 list_del(&port_ptr->siblings_client);
801 list_del(&port_ptr->siblings_graph);
803 log_info("removing port '%s':'%s' (%"PRIu64":%"PRIu64") from graph %s", client_ptr->name, port_ptr->name, client_ptr->id, port_ptr->id, graph_ptr->opath != NULL ? graph_ptr->opath : "JACK");
804 if (graph_ptr->opath != NULL && !port_ptr->hidden)
806 dbus_signal_emit(
807 g_dbus_connection,
808 graph_ptr->opath,
809 JACKDBUS_IFACE_PATCHBAY,
810 "PortDisappeared",
811 "ttsts",
812 &graph_ptr->graph_version,
813 &client_ptr->id,
814 &client_ptr->name,
815 &port_ptr->id,
816 &port_ptr->name);
819 free(port_ptr->name);
820 free(port_ptr);
823 static
824 void
825 ladish_graph_remove_client_internal(
826 struct ladish_graph * graph_ptr,
827 struct ladish_graph_client * client_ptr,
828 bool destroy_client,
829 bool destroy_ports)
831 struct ladish_graph_port * port_ptr;
833 while (!list_empty(&client_ptr->ports))
835 port_ptr = list_entry(client_ptr->ports.next, struct ladish_graph_port, siblings_client);
836 ladish_graph_remove_port_internal(graph_ptr, client_ptr, port_ptr, destroy_ports);
839 graph_ptr->graph_version++;
840 list_del(&client_ptr->siblings);
841 log_info("removing client '%s' (%"PRIu64") from graph %s", client_ptr->name, client_ptr->id, graph_ptr->opath != NULL ? graph_ptr->opath : "JACK");
842 if (graph_ptr->opath != NULL && !client_ptr->hidden)
844 dbus_signal_emit(
845 g_dbus_connection,
846 graph_ptr->opath,
847 JACKDBUS_IFACE_PATCHBAY,
848 "ClientDisappeared",
849 "tts",
850 &graph_ptr->graph_version,
851 &client_ptr->id,
852 &client_ptr->name);
855 free(client_ptr->name);
857 if (destroy_client)
859 ladish_client_destroy(client_ptr->client);
862 free(client_ptr);
865 #define graph_ptr ((struct ladish_graph *)graph_handle)
867 void ladish_graph_destroy(ladish_graph_handle graph_handle, bool destroy_ports)
869 ladish_graph_clear(graph_handle, destroy_ports);
870 ladish_dict_destroy(graph_ptr->dict);
871 if (graph_ptr->opath != NULL)
873 free(graph_ptr->opath);
875 free(graph_ptr);
878 void
879 ladish_graph_set_connection_handlers(
880 ladish_graph_handle graph_handle,
881 void * graph_context,
882 ladish_graph_connect_request_handler connect_handler,
883 ladish_graph_disconnect_request_handler disconnect_handler)
885 graph_ptr->context = graph_context;
886 graph_ptr->connect_handler = connect_handler;
887 graph_ptr->disconnect_handler = disconnect_handler;
890 void ladish_graph_clear(ladish_graph_handle graph_handle, bool destroy_ports)
892 struct ladish_graph_client * client_ptr;
893 struct ladish_graph_connection * connection_ptr;
895 log_info("ladish_graph_clear() called for graph '%s'", graph_ptr->opath != NULL ? graph_ptr->opath : "JACK");
897 while (!list_empty(&graph_ptr->connections))
899 connection_ptr = list_entry(graph_ptr->connections.next, struct ladish_graph_connection, siblings);
900 ladish_graph_remove_connection_internal(graph_ptr, connection_ptr);
903 while (!list_empty(&graph_ptr->clients))
905 client_ptr = list_entry(graph_ptr->clients.next, struct ladish_graph_client, siblings);
906 ladish_graph_remove_client_internal(graph_ptr, client_ptr, true, destroy_ports);
910 void * ladish_graph_get_dbus_context(ladish_graph_handle graph_handle)
912 return graph_handle;
915 ladish_dict_handle ladish_graph_get_dict(ladish_graph_handle graph_handle)
917 return graph_ptr->dict;
920 void ladish_graph_show_connection(ladish_graph_handle graph_handle, uint64_t connection_id)
922 struct ladish_graph_connection * connection_ptr;
924 log_info("ladish_graph_show_connection() called.");
926 connection_ptr = ladish_graph_find_connection_by_id(graph_ptr, connection_id);
927 if (connection_ptr == NULL)
929 ASSERT_NO_PASS;
930 return;
933 ASSERT(graph_ptr->opath != NULL);
934 ASSERT(connection_ptr->hidden);
935 connection_ptr->hidden = false;
936 connection_ptr->changing = false;
937 graph_ptr->graph_version++;
939 dbus_signal_emit(
940 g_dbus_connection,
941 graph_ptr->opath,
942 JACKDBUS_IFACE_PATCHBAY,
943 "PortsConnected",
944 "ttstststst",
945 &graph_ptr->graph_version,
946 &connection_ptr->port1_ptr->client_ptr->id,
947 &connection_ptr->port1_ptr->client_ptr->name,
948 &connection_ptr->port1_ptr->id,
949 &connection_ptr->port1_ptr->name,
950 &connection_ptr->port2_ptr->client_ptr->id,
951 &connection_ptr->port2_ptr->client_ptr->name,
952 &connection_ptr->port2_ptr->id,
953 &connection_ptr->port2_ptr->name,
954 &connection_ptr->id);
957 void ladish_graph_show_port(ladish_graph_handle graph_handle, ladish_port_handle port_handle)
959 struct ladish_graph_port * port_ptr;
961 //log_info("ladish_graph_show_port() called.");
963 port_ptr = ladish_graph_find_port(graph_ptr, port_handle);
964 if (port_ptr == NULL)
966 ASSERT_NO_PASS;
967 return;
970 //log_info("port '%s' is %s", port_ptr->name, port_ptr->hidden ? "invisible" : "visible");
972 if (port_ptr->client_ptr->hidden)
974 port_ptr->client_ptr->hidden = false;
975 graph_ptr->graph_version++;
976 if (graph_ptr->opath != NULL)
978 dbus_signal_emit(
979 g_dbus_connection,
980 graph_ptr->opath,
981 JACKDBUS_IFACE_PATCHBAY,
982 "ClientAppeared",
983 "tts",
984 &graph_ptr->graph_version,
985 &port_ptr->client_ptr->id,
986 &port_ptr->client_ptr->name);
990 ASSERT(port_ptr->hidden);
991 port_ptr->hidden = false;
992 graph_ptr->graph_version++;
993 if (graph_ptr->opath != NULL)
995 dbus_signal_emit(
996 g_dbus_connection,
997 graph_ptr->opath,
998 JACKDBUS_IFACE_PATCHBAY,
999 "PortAppeared",
1000 "ttstsuu",
1001 &graph_ptr->graph_version,
1002 &port_ptr->client_ptr->id,
1003 &port_ptr->client_ptr->name,
1004 &port_ptr->id,
1005 &port_ptr->name,
1006 &port_ptr->flags,
1007 &port_ptr->type);
1009 ladish_try_connect_hidden_connections(graph_handle);
1013 void ladish_graph_hide_port(ladish_graph_handle graph_handle, ladish_port_handle port_handle)
1015 struct ladish_graph_port * port_ptr;
1017 log_info("ladish_graph_hide_port() called.");
1019 port_ptr = ladish_graph_find_port(graph_ptr, port_handle);
1020 if (port_ptr == NULL)
1022 ASSERT_NO_PASS;
1023 return;
1026 log_info("Hidding port %"PRIu64, port_ptr->id);
1028 if (graph_ptr->opath != NULL)
1030 ladish_hide_connections(graph_ptr, port_ptr);
1033 ASSERT(!port_ptr->hidden);
1034 port_ptr->hidden = true;
1035 graph_ptr->graph_version++;
1037 if (graph_ptr->opath != NULL)
1039 dbus_signal_emit(
1040 g_dbus_connection,
1041 graph_ptr->opath,
1042 JACKDBUS_IFACE_PATCHBAY,
1043 "PortDisappeared",
1044 "ttsts",
1045 &graph_ptr->graph_version,
1046 &port_ptr->client_ptr->id,
1047 &port_ptr->client_ptr->name,
1048 &port_ptr->id,
1049 &port_ptr->name);
1053 void ladish_graph_hide_client(ladish_graph_handle graph_handle, ladish_client_handle client_handle)
1055 struct ladish_graph_client * client_ptr;
1057 log_info("ladish_graph_hide_client() called.");
1059 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1060 if (client_ptr == NULL)
1062 ASSERT_NO_PASS;
1063 return;
1066 ASSERT(!client_ptr->hidden);
1067 client_ptr->hidden = true;
1068 graph_ptr->graph_version++;
1070 if (graph_ptr->opath != NULL)
1072 dbus_signal_emit(
1073 g_dbus_connection,
1074 graph_ptr->opath,
1075 JACKDBUS_IFACE_PATCHBAY,
1076 "ClientDisappeared",
1077 "tts",
1078 &graph_ptr->graph_version,
1079 &client_ptr->id,
1080 &client_ptr->name);
1084 void ladish_graph_show_client(ladish_graph_handle graph_handle, ladish_client_handle client_handle)
1086 struct ladish_graph_client * client_ptr;
1088 log_info("ladish_graph_show_client() called.");
1090 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1091 if (client_ptr == NULL)
1093 ASSERT_NO_PASS;
1094 return;
1097 ASSERT(client_ptr->hidden);
1098 client_ptr->hidden = false;
1099 graph_ptr->graph_version++;
1101 if (graph_ptr->opath != NULL)
1103 dbus_signal_emit(
1104 g_dbus_connection,
1105 graph_ptr->opath,
1106 JACKDBUS_IFACE_PATCHBAY,
1107 "ClientAppeared",
1108 "tts",
1109 &graph_ptr->graph_version,
1110 &client_ptr->id,
1111 &client_ptr->name);
1115 void ladish_graph_adjust_port(ladish_graph_handle graph_handle, ladish_port_handle port_handle, uint32_t type, uint32_t flags)
1117 struct ladish_graph_port * port_ptr;
1119 //log_info("ladish_graph_adjust_port() called.");
1121 port_ptr = ladish_graph_find_port(graph_ptr, port_handle);
1122 if (port_ptr == NULL)
1124 ASSERT_NO_PASS;
1125 return;
1128 port_ptr->type = type;
1129 port_ptr->flags = flags;
1132 bool ladish_graph_add_client(ladish_graph_handle graph_handle, ladish_client_handle client_handle, const char * name, bool hidden)
1134 struct ladish_graph_client * client_ptr;
1136 log_info("adding client '%s' (%p) to graph %s", name, client_handle, graph_ptr->opath != NULL ? graph_ptr->opath : "JACK");
1138 client_ptr = malloc(sizeof(struct ladish_graph_client));
1139 if (client_ptr == NULL)
1141 log_error("malloc() failed for struct ladish_graph_client");
1142 return false;
1145 client_ptr->name = strdup(name);
1146 if (client_ptr->name == NULL)
1148 log_error("strdup() failed for graph client name");
1149 free(client_ptr);
1150 return false;
1153 client_ptr->id = graph_ptr->next_client_id++;
1154 client_ptr->client = client_handle;
1155 client_ptr->hidden = hidden;
1156 graph_ptr->graph_version++;
1158 INIT_LIST_HEAD(&client_ptr->ports);
1160 list_add_tail(&client_ptr->siblings, &graph_ptr->clients);
1162 if (!hidden && graph_ptr->opath != NULL)
1164 dbus_signal_emit(
1165 g_dbus_connection,
1166 graph_ptr->opath,
1167 JACKDBUS_IFACE_PATCHBAY,
1168 "ClientAppeared",
1169 "tts",
1170 &graph_ptr->graph_version,
1171 &client_ptr->id,
1172 &client_ptr->name);
1175 return true;
1178 void
1179 ladish_graph_remove_client(
1180 ladish_graph_handle graph_handle,
1181 ladish_client_handle client_handle,
1182 bool destroy_ports)
1184 struct ladish_graph_client * client_ptr;
1186 log_info("ladish_graph_remove_client() called.");
1188 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1189 if (client_ptr != NULL)
1191 ladish_graph_remove_client_internal(graph_ptr, client_ptr, false, destroy_ports);
1193 else
1195 ASSERT_NO_PASS;
1199 bool
1200 ladish_graph_add_port(
1201 ladish_graph_handle graph_handle,
1202 ladish_client_handle client_handle,
1203 ladish_port_handle port_handle,
1204 const char * name,
1205 uint32_t type,
1206 uint32_t flags,
1207 bool hidden)
1209 struct ladish_graph_client * client_ptr;
1210 struct ladish_graph_port * port_ptr;
1212 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1213 if (client_ptr == NULL)
1215 log_error("cannot find client to add port to");
1216 ASSERT_NO_PASS;
1217 return false;
1220 log_info("adding port '%s':'%s' (%p) to graph %s", client_ptr->name, name, port_handle, graph_ptr->opath != NULL ? graph_ptr->opath : "JACK");
1222 port_ptr = malloc(sizeof(struct ladish_graph_port));
1223 if (port_ptr == NULL)
1225 log_error("malloc() failed for struct ladish_graph_port");
1226 return false;
1229 port_ptr->name = strdup(name);
1230 if (port_ptr->name == NULL)
1232 log_error("strdup() failed for graph port name");
1233 free(port_ptr);
1234 return false;
1237 port_ptr->type = type;
1238 port_ptr->flags = flags;
1240 port_ptr->id = graph_ptr->next_port_id++;
1241 port_ptr->port = port_handle;
1242 port_ptr->hidden = hidden;
1243 graph_ptr->graph_version++;
1245 port_ptr->client_ptr = client_ptr;
1246 list_add_tail(&port_ptr->siblings_client, &client_ptr->ports);
1247 list_add_tail(&port_ptr->siblings_graph, &graph_ptr->ports);
1249 if (!hidden && graph_ptr->opath != NULL)
1251 dbus_signal_emit(
1252 g_dbus_connection,
1253 graph_ptr->opath,
1254 JACKDBUS_IFACE_PATCHBAY,
1255 "PortAppeared",
1256 "ttstsuu",
1257 &graph_ptr->graph_version,
1258 &client_ptr->id,
1259 &client_ptr->name,
1260 &port_ptr->id,
1261 &port_ptr->name,
1262 &flags,
1263 &type);
1266 return true;
1269 uint64_t
1270 ladish_graph_add_connection(
1271 ladish_graph_handle graph_handle,
1272 ladish_port_handle port1_handle,
1273 ladish_port_handle port2_handle,
1274 bool hidden)
1276 struct ladish_graph_port * port1_ptr;
1277 struct ladish_graph_port * port2_ptr;
1278 struct ladish_graph_connection * connection_ptr;
1280 port1_ptr = ladish_graph_find_port(graph_ptr, port1_handle);
1281 ASSERT(port1_ptr != NULL);
1282 port2_ptr = ladish_graph_find_port(graph_ptr, port2_handle);
1283 ASSERT(port2_ptr != NULL);
1285 connection_ptr = malloc(sizeof(struct ladish_graph_connection));
1286 if (connection_ptr == NULL)
1288 log_error("malloc() failed for struct ladish_graph_connection");
1289 return 0;
1292 if (!ladish_dict_create(&connection_ptr->dict))
1294 log_error("ladish_dict_create() failed for connection");
1295 free(connection_ptr);
1296 return 0;
1299 connection_ptr->id = graph_ptr->next_connection_id++;
1300 connection_ptr->port1_ptr = port1_ptr;
1301 connection_ptr->port2_ptr = port2_ptr;
1302 connection_ptr->hidden = hidden;
1303 connection_ptr->changing = false;
1304 graph_ptr->graph_version++;
1306 list_add_tail(&connection_ptr->siblings, &graph_ptr->connections);
1308 /* log_info( */
1309 /* "new connection %"PRIu64" between '%s':'%s' and '%s':'%s'", */
1310 /* connection_ptr->id, */
1311 /* port1_ptr->client_ptr->name, */
1312 /* port1_ptr->name, */
1313 /* port2_ptr->client_ptr->name, */
1314 /* port2_ptr->name); */
1316 if (!hidden && graph_ptr->opath != NULL)
1318 dbus_signal_emit(
1319 g_dbus_connection,
1320 graph_ptr->opath,
1321 JACKDBUS_IFACE_PATCHBAY,
1322 "PortsConnected",
1323 "ttstststst",
1324 &graph_ptr->graph_version,
1325 &port1_ptr->client_ptr->id,
1326 &port1_ptr->client_ptr->name,
1327 &port1_ptr->id,
1328 &port1_ptr->name,
1329 &port2_ptr->client_ptr->id,
1330 &port2_ptr->client_ptr->name,
1331 &port2_ptr->id,
1332 &port2_ptr->name,
1333 &connection_ptr->id);
1336 return connection_ptr->id;
1339 void
1340 ladish_graph_remove_connection(
1341 ladish_graph_handle graph_handle,
1342 uint64_t connection_id)
1344 struct ladish_graph_connection * connection_ptr;
1346 connection_ptr = ladish_graph_find_connection_by_id(graph_ptr, connection_id);
1347 if (connection_ptr == NULL)
1349 ASSERT_NO_PASS;
1350 return;
1353 if (connection_ptr->changing)
1355 /* log_info( */
1356 /* "removing connection '%s':'%s' - '%s':'%s'", */
1357 /* connection_ptr->port1_ptr->client_ptr->name, */
1358 /* connection_ptr->port1_ptr->name, */
1359 /* connection_ptr->port2_ptr->client_ptr->name, */
1360 /* connection_ptr->port2_ptr->name); */
1362 ladish_graph_remove_connection_internal(graph_ptr, connection_ptr);
1364 else
1366 /* log_info( */
1367 /* "hiding connection '%s':'%s' - '%s':'%s'", */
1368 /* connection_ptr->port1_ptr->client_ptr->name, */
1369 /* connection_ptr->port1_ptr->name, */
1370 /* connection_ptr->port2_ptr->client_ptr->name, */
1371 /* connection_ptr->port2_ptr->name); */
1373 ladish_graph_hide_connection_internal(graph_ptr, connection_ptr);
1377 bool
1378 ladish_graph_get_connection_ports(
1379 ladish_graph_handle graph_handle,
1380 uint64_t connection_id,
1381 ladish_port_handle * port1_handle_ptr,
1382 ladish_port_handle * port2_handle_ptr)
1384 struct ladish_graph_connection * connection_ptr;
1386 connection_ptr = ladish_graph_find_connection_by_id(graph_ptr, connection_id);
1387 if (connection_ptr == NULL)
1389 return false;
1392 *port1_handle_ptr = connection_ptr->port1_ptr->port;
1393 *port2_handle_ptr = connection_ptr->port2_ptr->port;
1395 return true;
1398 ladish_dict_handle ladish_graph_get_connection_dict(ladish_graph_handle graph_handle, uint64_t connection_id)
1400 struct ladish_graph_connection * connection_ptr;
1402 connection_ptr = ladish_graph_find_connection_by_id(graph_ptr, connection_id);
1403 if (connection_ptr == NULL)
1405 return NULL;
1408 return connection_ptr->dict;
1411 bool
1412 ladish_graph_find_connection(
1413 ladish_graph_handle graph_handle,
1414 ladish_port_handle port1_handle,
1415 ladish_port_handle port2_handle,
1416 uint64_t * connection_id_ptr)
1418 struct ladish_graph_port * port1_ptr;
1419 struct ladish_graph_port * port2_ptr;
1420 struct ladish_graph_connection * connection_ptr;
1422 port1_ptr = ladish_graph_find_port(graph_ptr, port1_handle);
1423 if (port1_ptr == NULL)
1425 return false;
1428 port2_ptr = ladish_graph_find_port(graph_ptr, port2_handle);
1429 if (port1_ptr == NULL)
1431 return false;
1434 connection_ptr = ladish_graph_find_connection_by_ports(graph_ptr, port1_ptr, port2_ptr);
1435 if (connection_ptr == NULL)
1437 return false;
1440 *connection_id_ptr = connection_ptr->id;
1442 return true;
1445 ladish_client_handle ladish_graph_find_client_by_name(ladish_graph_handle graph_handle, const char * name)
1447 struct list_head * node_ptr;
1448 struct ladish_graph_client * client_ptr;
1450 list_for_each(node_ptr, &graph_ptr->clients)
1452 client_ptr = list_entry(node_ptr, struct ladish_graph_client, siblings);
1453 if (strcmp(client_ptr->name, name) == 0)
1455 return client_ptr->client;
1459 return NULL;
1462 ladish_port_handle ladish_graph_find_port_by_name(ladish_graph_handle graph_handle, ladish_client_handle client_handle, const char * name)
1464 struct ladish_graph_client * client_ptr;
1465 struct list_head * node_ptr;
1466 struct ladish_graph_port * port_ptr;
1468 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1469 if (client_ptr != NULL)
1471 list_for_each(node_ptr, &client_ptr->ports)
1473 port_ptr = list_entry(node_ptr, struct ladish_graph_port, siblings_client);
1474 if (strcmp(port_ptr->name, name) == 0)
1476 return port_ptr->port;
1480 else
1482 ASSERT_NO_PASS;
1485 return NULL;
1488 ladish_client_handle ladish_graph_find_client_by_uuid(ladish_graph_handle graph_handle, uuid_t uuid)
1490 struct list_head * node_ptr;
1491 struct ladish_graph_client * client_ptr;
1492 uuid_t current_uuid;
1494 list_for_each(node_ptr, &graph_ptr->clients)
1496 client_ptr = list_entry(node_ptr, struct ladish_graph_client, siblings);
1497 ladish_client_get_uuid(client_ptr->client, current_uuid);
1498 if (uuid_compare(current_uuid, uuid) == 0)
1500 return client_ptr->client;
1504 return NULL;
1507 ladish_port_handle ladish_graph_find_port_by_uuid(ladish_graph_handle graph_handle, uuid_t uuid)
1509 struct list_head * node_ptr;
1510 struct ladish_graph_port * port_ptr;
1511 uuid_t current_uuid;
1513 list_for_each(node_ptr, &graph_ptr->ports)
1515 port_ptr = list_entry(node_ptr, struct ladish_graph_port, siblings_graph);
1516 ladish_port_get_uuid(port_ptr->port, current_uuid);
1517 if (uuid_compare(current_uuid, uuid) == 0)
1519 return port_ptr->port;
1523 return NULL;
1526 ladish_client_handle ladish_graph_get_port_client(ladish_graph_handle graph_handle, ladish_port_handle port_handle)
1528 struct ladish_graph_port * port_ptr;
1530 port_ptr = ladish_graph_find_port(graph_ptr, port_handle);
1531 if (port_ptr == NULL)
1533 return NULL;
1536 return port_ptr->client_ptr->client;
1539 bool ladish_graph_is_port_present(ladish_graph_handle graph_handle, ladish_port_handle port_handle)
1541 return ladish_graph_find_port(graph_ptr, port_handle) != NULL;
1544 ladish_client_handle ladish_graph_find_client_by_id(ladish_graph_handle graph_handle, uint64_t client_id)
1546 struct list_head * node_ptr;
1547 struct ladish_graph_client * client_ptr;
1549 list_for_each(node_ptr, &graph_ptr->clients)
1551 client_ptr = list_entry(node_ptr, struct ladish_graph_client, siblings);
1552 if (client_ptr->id == client_id)
1554 return client_ptr->client;
1558 return NULL;
1561 ladish_port_handle ladish_graph_find_port_by_id(ladish_graph_handle graph_handle, uint64_t port_id)
1563 struct ladish_graph_port * port_ptr;
1565 port_ptr = ladish_graph_find_port_by_id_internal(graph_ptr, port_id);
1566 if (port_ptr == NULL)
1568 return NULL;
1571 return port_ptr->port;
1574 ladish_client_handle ladish_graph_find_client_by_jack_id(ladish_graph_handle graph_handle, uint64_t client_id)
1576 struct list_head * node_ptr;
1577 struct ladish_graph_client * client_ptr;
1579 list_for_each(node_ptr, &graph_ptr->clients)
1581 client_ptr = list_entry(node_ptr, struct ladish_graph_client, siblings);
1582 if (ladish_client_get_jack_id(client_ptr->client) == client_id)
1584 return client_ptr->client;
1588 return NULL;
1591 ladish_port_handle ladish_graph_find_port_by_jack_id(ladish_graph_handle graph_handle, uint64_t port_id)
1593 struct list_head * node_ptr;
1594 struct ladish_graph_port * port_ptr;
1596 list_for_each(node_ptr, &graph_ptr->ports)
1598 port_ptr = list_entry(node_ptr, struct ladish_graph_port, siblings_graph);
1599 if (ladish_port_get_jack_id(port_ptr->port) == port_id)
1601 return port_ptr->port;
1605 return NULL;
1608 ladish_client_handle
1609 ladish_graph_remove_port(
1610 ladish_graph_handle graph_handle,
1611 ladish_port_handle port)
1613 struct ladish_graph_port * port_ptr;
1615 port_ptr = ladish_graph_find_port(graph_ptr, port);
1616 if (port_ptr == NULL)
1618 return NULL;
1621 ladish_graph_remove_port_internal(graph_ptr, port_ptr->client_ptr, port_ptr, false);
1622 return port_ptr->client_ptr->client;
1625 const char * ladish_graph_get_client_name(ladish_graph_handle graph_handle, ladish_client_handle client_handle)
1627 struct ladish_graph_client * client_ptr;
1629 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1630 if (client_ptr != NULL)
1632 return client_ptr->name;
1635 ASSERT_NO_PASS;
1636 return NULL;
1639 bool ladish_graph_is_client_empty(ladish_graph_handle graph_handle, ladish_client_handle client_handle)
1641 struct ladish_graph_client * client_ptr;
1643 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1644 if (client_ptr != NULL)
1646 return list_empty(&client_ptr->ports);
1649 ASSERT_NO_PASS;
1650 return true;
1653 bool ladish_graph_is_client_looks_empty(ladish_graph_handle graph_handle, ladish_client_handle client_handle)
1655 struct ladish_graph_client * client_ptr;
1656 struct list_head * node_ptr;
1657 struct ladish_graph_port * port_ptr;
1659 client_ptr = ladish_graph_find_client(graph_ptr, client_handle);
1660 if (client_ptr != NULL)
1662 list_for_each(node_ptr, &client_ptr->ports)
1664 port_ptr = list_entry(node_ptr, struct ladish_graph_port, siblings_client);
1665 if (!port_ptr->hidden)
1667 log_info("port '%s' is visible, client '%s' does not look empty", port_ptr->name, client_ptr->name);
1668 return false;
1670 else
1672 log_info("port '%s' is invisible", port_ptr->name);
1676 log_info("client '%s' looks empty", client_ptr->name);
1677 return true;
1680 ASSERT_NO_PASS;
1681 return true;
1684 void ladish_try_connect_hidden_connections(ladish_graph_handle graph_handle)
1686 struct list_head * node_ptr;
1687 struct ladish_graph_connection * connection_ptr;
1689 if (graph_ptr->connect_handler == NULL)
1691 ASSERT_NO_PASS;
1692 return;
1695 ASSERT(graph_ptr->opath != NULL);
1697 list_for_each(node_ptr, &graph_ptr->connections)
1699 connection_ptr = list_entry(node_ptr, struct ladish_graph_connection, siblings);
1700 if (connection_ptr->hidden &&
1701 !connection_ptr->changing &&
1702 !connection_ptr->port1_ptr->hidden &&
1703 !connection_ptr->port2_ptr->hidden)
1705 log_info(
1706 "auto connecting '%s':'%s' to '%s':'%s'",
1707 connection_ptr->port1_ptr->client_ptr->name,
1708 connection_ptr->port1_ptr->name,
1709 connection_ptr->port2_ptr->client_ptr->name,
1710 connection_ptr->port2_ptr->name);
1712 connection_ptr->changing = true;
1713 if (!graph_ptr->connect_handler(graph_ptr->context, graph_handle, connection_ptr->port1_ptr->port, connection_ptr->port2_ptr->port))
1715 connection_ptr->changing = false;
1716 log_error("auto connect failed.");
1722 bool
1723 ladish_graph_iterate_nodes(
1724 ladish_graph_handle graph_handle,
1725 void * callback_context,
1726 bool
1727 (* client_begin_callback)(
1728 void * context,
1729 ladish_client_handle client_handle,
1730 const char * client_name,
1731 void ** client_iteration_context_ptr_ptr),
1732 bool
1733 (* port_callback)(
1734 void * context,
1735 void * client_iteration_context_ptr,
1736 ladish_client_handle client_handle,
1737 const char * client_name,
1738 ladish_port_handle port_handle,
1739 const char * port_name,
1740 uint32_t port_type,
1741 uint32_t port_flags),
1742 bool
1743 (* client_end_callback)(
1744 void * context,
1745 ladish_client_handle client_handle,
1746 const char * client_name,
1747 void * client_iteration_context_ptr))
1749 struct list_head * client_node_ptr;
1750 struct ladish_graph_client * client_ptr;
1751 void * client_context;
1752 struct list_head * port_node_ptr;
1753 struct ladish_graph_port * port_ptr;
1755 list_for_each(client_node_ptr, &graph_ptr->clients)
1757 client_ptr = list_entry(client_node_ptr, struct ladish_graph_client, siblings);
1758 if (!client_begin_callback(callback_context, client_ptr->client, client_ptr->name, &client_context))
1760 return false;
1763 if (client_ptr->hidden)
1765 continue;
1768 list_for_each(port_node_ptr, &client_ptr->ports)
1770 port_ptr = list_entry(port_node_ptr, struct ladish_graph_port, siblings_client);
1772 if (port_ptr->hidden)
1774 continue;
1777 if (!port_callback(
1778 callback_context,
1779 client_context,
1780 client_ptr->client,
1781 client_ptr->name,
1782 port_ptr->port,
1783 port_ptr->name,
1784 port_ptr->type,
1785 port_ptr->flags))
1787 return false;
1791 if (!client_end_callback(callback_context, client_ptr->client, client_ptr->name, &client_context))
1793 return false;
1797 return true;
1800 bool
1801 ladish_graph_iterate_connections(
1802 ladish_graph_handle graph_handle,
1803 void * callback_context,
1804 bool (* callback)(void * context, ladish_port_handle port1_handle, ladish_port_handle port2_handle, ladish_dict_handle dict))
1806 struct list_head * node_ptr;
1807 struct ladish_graph_connection * connection_ptr;
1809 list_for_each(node_ptr, &graph_ptr->connections)
1811 connection_ptr = list_entry(node_ptr, struct ladish_graph_connection, siblings);
1813 if (connection_ptr->hidden)
1815 continue;
1818 if (!callback(callback_context, connection_ptr->port1_ptr->port, connection_ptr->port2_ptr->port, connection_ptr->dict))
1820 return false;
1824 return true;
1827 static
1828 bool
1829 dump_dict_entry(
1830 void * context,
1831 const char * key,
1832 const char * value)
1834 log_info("%s key '%s' with value '%s'", (const char *)context, key, value);
1835 return true;
1838 static
1839 void
1840 dump_dict(
1841 const char * indent,
1842 ladish_dict_handle dict)
1844 if (ladish_dict_is_empty(dict))
1846 return;
1849 log_info("%sdict:", indent);
1850 ladish_dict_iterate(dict, (void *)indent, dump_dict_entry);
1853 void ladish_graph_dump(ladish_graph_handle graph_handle)
1855 struct list_head * client_node_ptr;
1856 struct ladish_graph_client * client_ptr;
1857 struct list_head * port_node_ptr;
1858 struct ladish_graph_port * port_ptr;
1859 struct list_head * connection_node_ptr;
1860 struct ladish_graph_connection * connection_ptr;
1862 log_info("graph %s", graph_ptr->opath != NULL ? graph_ptr->opath : "JACK");
1863 log_info(" version %"PRIu64, graph_ptr->graph_version);
1864 dump_dict(" ", graph_ptr->dict);
1865 log_info(" clients:");
1866 list_for_each(client_node_ptr, &graph_ptr->clients)
1868 client_ptr = list_entry(client_node_ptr, struct ladish_graph_client, siblings);
1869 log_info(" %s client '%s', id=%"PRIu64", ptr=%p", client_ptr->hidden ? "invisible" : "visible", client_ptr->name, client_ptr->id, client_ptr->client);
1870 dump_dict(" ", ladish_client_get_dict(client_ptr->client));
1871 log_info(" ports:");
1872 list_for_each(port_node_ptr, &client_ptr->ports)
1874 port_ptr = list_entry(port_node_ptr, struct ladish_graph_port, siblings_client);
1876 log_info(" %s port '%s', id=%"PRIu64", type=0x%"PRIX32", flags=0x%"PRIX32", ptr=%p", port_ptr->hidden ? "invisible" : "visible", port_ptr->name, port_ptr->id, port_ptr->type, port_ptr->flags, port_ptr->port);
1877 dump_dict(" ", ladish_port_get_dict(port_ptr->port));
1880 log_info(" connections:");
1881 list_for_each(connection_node_ptr, &graph_ptr->connections)
1883 connection_ptr = list_entry(connection_node_ptr, struct ladish_graph_connection, siblings);
1885 log_info(
1886 " %s connection '%s':'%s' - '%s':'%s'%s",
1887 connection_ptr->hidden ? "invisible" : "visible",
1888 connection_ptr->port1_ptr->client_ptr->name,
1889 connection_ptr->port1_ptr->name,
1890 connection_ptr->port2_ptr->client_ptr->name,
1891 connection_ptr->port2_ptr->name,
1892 connection_ptr->changing ? " [changing]" : "");
1893 dump_dict(" ", ladish_port_get_dict(port_ptr->port));
1897 #undef graph_ptr
1899 METHOD_ARGS_BEGIN(GetAllPorts, "Get all ports")
1900 METHOD_ARG_DESCRIBE_IN("ports_list", "as", "List of all ports")
1901 METHOD_ARGS_END
1903 METHOD_ARGS_BEGIN(GetGraph, "Get whole graph")
1904 METHOD_ARG_DESCRIBE_IN("known_graph_version", DBUS_TYPE_UINT64_AS_STRING, "Known graph version")
1905 METHOD_ARG_DESCRIBE_OUT("current_graph_version", DBUS_TYPE_UINT64_AS_STRING, "Current graph version")
1906 METHOD_ARG_DESCRIBE_OUT("clients_and_ports", "a(tsa(tsuu))", "Clients and their ports")
1907 METHOD_ARG_DESCRIBE_OUT("connections", "a(tstststst)", "Connections array")
1908 METHOD_ARGS_END
1910 METHOD_ARGS_BEGIN(ConnectPortsByName, "Connect ports")
1911 METHOD_ARG_DESCRIBE_IN("client1_name", DBUS_TYPE_STRING_AS_STRING, "name first port client")
1912 METHOD_ARG_DESCRIBE_IN("port1_name", DBUS_TYPE_STRING_AS_STRING, "name of first port")
1913 METHOD_ARG_DESCRIBE_IN("client2_name", DBUS_TYPE_STRING_AS_STRING, "name second port client")
1914 METHOD_ARG_DESCRIBE_IN("port2_name", DBUS_TYPE_STRING_AS_STRING, "name of second port")
1915 METHOD_ARGS_END
1917 METHOD_ARGS_BEGIN(ConnectPortsByID, "Connect ports")
1918 METHOD_ARG_DESCRIBE_IN("port1_id", DBUS_TYPE_UINT64_AS_STRING, "id of first port")
1919 METHOD_ARG_DESCRIBE_IN("port2_id", DBUS_TYPE_UINT64_AS_STRING, "if of second port")
1920 METHOD_ARGS_END
1922 METHOD_ARGS_BEGIN(DisconnectPortsByName, "Disconnect ports")
1923 METHOD_ARG_DESCRIBE_IN("client1_name", DBUS_TYPE_STRING_AS_STRING, "name first port client")
1924 METHOD_ARG_DESCRIBE_IN("port1_name", DBUS_TYPE_STRING_AS_STRING, "name of first port")
1925 METHOD_ARG_DESCRIBE_IN("client2_name", DBUS_TYPE_STRING_AS_STRING, "name second port client")
1926 METHOD_ARG_DESCRIBE_IN("port2_name", DBUS_TYPE_STRING_AS_STRING, "name of second port")
1927 METHOD_ARGS_END
1929 METHOD_ARGS_BEGIN(DisconnectPortsByID, "Disconnect ports")
1930 METHOD_ARG_DESCRIBE_IN("port1_id", DBUS_TYPE_UINT64_AS_STRING, "id of first port")
1931 METHOD_ARG_DESCRIBE_IN("port2_id", DBUS_TYPE_UINT64_AS_STRING, "if of second port")
1932 METHOD_ARGS_END
1934 METHOD_ARGS_BEGIN(DisconnectPortsByConnectionID, "Disconnect ports")
1935 METHOD_ARG_DESCRIBE_IN("connection_id", DBUS_TYPE_UINT64_AS_STRING, "id of connection to disconnect")
1936 METHOD_ARGS_END
1938 METHOD_ARGS_BEGIN(GetClientPID, "get process id of client")
1939 METHOD_ARG_DESCRIBE_IN("client_id", DBUS_TYPE_UINT64_AS_STRING, "id of client")
1940 METHOD_ARG_DESCRIBE_OUT("process_id", DBUS_TYPE_INT64_AS_STRING, "pid of client")
1941 METHOD_ARGS_END
1943 METHODS_BEGIN
1944 METHOD_DESCRIBE(GetAllPorts, get_all_ports)
1945 METHOD_DESCRIBE(GetGraph, get_graph)
1946 METHOD_DESCRIBE(ConnectPortsByName, connect_ports_by_name)
1947 METHOD_DESCRIBE(ConnectPortsByID, connect_ports_by_id)
1948 METHOD_DESCRIBE(DisconnectPortsByName, disconnect_ports_by_name)
1949 METHOD_DESCRIBE(DisconnectPortsByID, disconnect_ports_by_id)
1950 METHOD_DESCRIBE(DisconnectPortsByConnectionID, disconnect_ports_by_connection_id)
1951 METHOD_DESCRIBE(GetClientPID, get_client_pid)
1952 METHODS_END
1954 SIGNAL_ARGS_BEGIN(GraphChanged, "")
1955 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING, "")
1956 SIGNAL_ARGS_END
1958 SIGNAL_ARGS_BEGIN(ClientAppeared, "")
1959 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING, "")
1960 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING, "")
1961 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING, "")
1962 SIGNAL_ARGS_END
1964 SIGNAL_ARGS_BEGIN(ClientDisappeared, "")
1965 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING, "")
1966 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING, "")
1967 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING, "")
1968 SIGNAL_ARGS_END
1970 SIGNAL_ARGS_BEGIN(PortAppeared, "")
1971 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING, "")
1972 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING, "")
1973 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING, "")
1974 SIGNAL_ARG_DESCRIBE("port_id", DBUS_TYPE_UINT64_AS_STRING, "")
1975 SIGNAL_ARG_DESCRIBE("port_name", DBUS_TYPE_STRING_AS_STRING, "")
1976 SIGNAL_ARG_DESCRIBE("port_flags", DBUS_TYPE_UINT32_AS_STRING, "")
1977 SIGNAL_ARG_DESCRIBE("port_type", DBUS_TYPE_UINT32_AS_STRING, "")
1978 SIGNAL_ARGS_END
1980 SIGNAL_ARGS_BEGIN(PortDisappeared, "")
1981 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING, "")
1982 SIGNAL_ARG_DESCRIBE("client_id", DBUS_TYPE_UINT64_AS_STRING, "")
1983 SIGNAL_ARG_DESCRIBE("client_name", DBUS_TYPE_STRING_AS_STRING, "")
1984 SIGNAL_ARG_DESCRIBE("port_id", DBUS_TYPE_UINT64_AS_STRING, "")
1985 SIGNAL_ARG_DESCRIBE("port_name", DBUS_TYPE_STRING_AS_STRING, "")
1986 SIGNAL_ARGS_END
1988 SIGNAL_ARGS_BEGIN(PortsConnected, "")
1989 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING, "")
1990 SIGNAL_ARG_DESCRIBE("client1_id", DBUS_TYPE_UINT64_AS_STRING, "")
1991 SIGNAL_ARG_DESCRIBE("client1_name", DBUS_TYPE_STRING_AS_STRING, "")
1992 SIGNAL_ARG_DESCRIBE("port1_id", DBUS_TYPE_UINT64_AS_STRING, "")
1993 SIGNAL_ARG_DESCRIBE("port1_name", DBUS_TYPE_STRING_AS_STRING, "")
1994 SIGNAL_ARG_DESCRIBE("client2_id", DBUS_TYPE_UINT64_AS_STRING, "")
1995 SIGNAL_ARG_DESCRIBE("client2_name", DBUS_TYPE_STRING_AS_STRING, "")
1996 SIGNAL_ARG_DESCRIBE("port2_id", DBUS_TYPE_UINT64_AS_STRING, "")
1997 SIGNAL_ARG_DESCRIBE("port2_name", DBUS_TYPE_STRING_AS_STRING, "")
1998 SIGNAL_ARG_DESCRIBE("connection_id", DBUS_TYPE_UINT64_AS_STRING, "")
1999 SIGNAL_ARGS_END
2001 SIGNAL_ARGS_BEGIN(PortsDisconnected, "")
2002 SIGNAL_ARG_DESCRIBE("new_graph_version", DBUS_TYPE_UINT64_AS_STRING, "")
2003 SIGNAL_ARG_DESCRIBE("client1_id", DBUS_TYPE_UINT64_AS_STRING, "")
2004 SIGNAL_ARG_DESCRIBE("client1_name", DBUS_TYPE_STRING_AS_STRING, "")
2005 SIGNAL_ARG_DESCRIBE("port1_id", DBUS_TYPE_UINT64_AS_STRING, "")
2006 SIGNAL_ARG_DESCRIBE("port1_name", DBUS_TYPE_STRING_AS_STRING, "")
2007 SIGNAL_ARG_DESCRIBE("client2_id", DBUS_TYPE_UINT64_AS_STRING, "")
2008 SIGNAL_ARG_DESCRIBE("client2_name", DBUS_TYPE_STRING_AS_STRING, "")
2009 SIGNAL_ARG_DESCRIBE("port2_id", DBUS_TYPE_UINT64_AS_STRING, "")
2010 SIGNAL_ARG_DESCRIBE("port2_name", DBUS_TYPE_STRING_AS_STRING, "")
2011 SIGNAL_ARG_DESCRIBE("connection_id", DBUS_TYPE_UINT64_AS_STRING, "")
2012 SIGNAL_ARGS_END
2014 SIGNALS_BEGIN
2015 SIGNAL_DESCRIBE(GraphChanged)
2016 SIGNAL_DESCRIBE(ClientAppeared)
2017 SIGNAL_DESCRIBE(ClientDisappeared)
2018 SIGNAL_DESCRIBE(PortAppeared)
2019 SIGNAL_DESCRIBE(PortDisappeared)
2020 SIGNAL_DESCRIBE(PortsConnected)
2021 SIGNAL_DESCRIBE(PortsDisconnected)
2022 SIGNALS_END
2024 INTERFACE_BEGIN(g_interface_patchbay, JACKDBUS_IFACE_PATCHBAY)
2025 INTERFACE_DEFAULT_HANDLER
2026 INTERFACE_EXPOSE_METHODS
2027 INTERFACE_EXPOSE_SIGNALS
2028 INTERFACE_END