Fix symbols export in ringbuffer.c.
[jack2.git] / dbus / controller_iface_patchbay.c
blob8439140441a9ed7f3b48f87b03426128a6a8f261
1 /* -*- Mode: C ; c-basic-offset: 4 -*- */
2 /*
3 Copyright (C) 2008 Nedko Arnaudov
4 Copyright (C) 2008 Juuso Alasuutari
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #if defined(HAVE_CONFIG_H)
22 #include "config.h"
23 #endif
25 #define _GNU_SOURCE /* PTHREAD_MUTEX_RECURSIVE */
27 #include <stdint.h>
28 #include <inttypes.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <dbus/dbus.h>
33 #include <pthread.h>
35 #include "jackdbus.h"
36 #include "controller_internal.h"
37 #include "list.h"
39 #define JACK_DBUS_IFACE_NAME "org.jackaudio.JackPatchbay"
41 /* FIXME: these need to be retrieved from common headers */
42 #define JACK_CLIENT_NAME_SIZE 64
43 #define JACK_PORT_NAME_SIZE 256
45 struct jack_graph
47 uint64_t version;
48 struct list_head clients;
49 struct list_head ports;
50 struct list_head connections;
53 struct jack_graph_client
55 uint64_t id;
56 char * name;
57 int pid;
58 struct list_head siblings;
59 struct list_head ports;
62 struct jack_graph_port
64 uint64_t id;
65 char * name;
66 uint32_t flags;
67 uint32_t type;
68 struct list_head siblings_graph;
69 struct list_head siblings_client;
70 struct jack_graph_client * client;
73 struct jack_graph_connection
75 uint64_t id;
76 struct jack_graph_port * port1;
77 struct jack_graph_port * port2;
78 struct list_head siblings;
81 struct jack_controller_patchbay
83 pthread_mutex_t lock;
84 struct jack_graph graph;
85 uint64_t next_client_id;
86 uint64_t next_port_id;
87 uint64_t next_connection_id;
90 void
91 jack_controller_patchbay_send_signal_graph_changed(
92 dbus_uint64_t new_graph_version)
95 jack_dbus_send_signal(
96 JACK_CONTROLLER_OBJECT_PATH,
97 JACK_DBUS_IFACE_NAME,
98 "GraphChanged",
99 DBUS_TYPE_UINT64,
100 &new_graph_version,
101 DBUS_TYPE_INVALID);
104 void
105 jack_controller_patchbay_send_signal_client_appeared(
106 dbus_uint64_t new_graph_version,
107 dbus_uint64_t client_id,
108 const char * client_name)
111 jack_dbus_send_signal(
112 JACK_CONTROLLER_OBJECT_PATH,
113 JACK_DBUS_IFACE_NAME,
114 "ClientAppeared",
115 DBUS_TYPE_UINT64,
116 &new_graph_version,
117 DBUS_TYPE_UINT64,
118 &client_id,
119 DBUS_TYPE_STRING,
120 &client_name,
121 DBUS_TYPE_INVALID);
124 void
125 jack_controller_patchbay_send_signal_client_disappeared(
126 dbus_uint64_t new_graph_version,
127 dbus_uint64_t client_id,
128 const char * client_name)
131 jack_dbus_send_signal(
132 JACK_CONTROLLER_OBJECT_PATH,
133 JACK_DBUS_IFACE_NAME,
134 "ClientDisappeared",
135 DBUS_TYPE_UINT64,
136 &new_graph_version,
137 DBUS_TYPE_UINT64,
138 &client_id,
139 DBUS_TYPE_STRING,
140 &client_name,
141 DBUS_TYPE_INVALID);
144 void
145 jack_controller_patchbay_send_signal_port_appeared(
146 dbus_uint64_t new_graph_version,
147 dbus_uint64_t client_id,
148 const char * client_name,
149 dbus_uint64_t port_id,
150 const char * port_name,
151 dbus_uint32_t port_flags,
152 dbus_uint32_t port_type)
155 jack_dbus_send_signal(
156 JACK_CONTROLLER_OBJECT_PATH,
157 JACK_DBUS_IFACE_NAME,
158 "PortAppeared",
159 DBUS_TYPE_UINT64,
160 &new_graph_version,
161 DBUS_TYPE_UINT64,
162 &client_id,
163 DBUS_TYPE_STRING,
164 &client_name,
165 DBUS_TYPE_UINT64,
166 &port_id,
167 DBUS_TYPE_STRING,
168 &port_name,
169 DBUS_TYPE_UINT32,
170 &port_flags,
171 DBUS_TYPE_UINT32,
172 &port_type,
173 DBUS_TYPE_INVALID);
176 void
177 jack_controller_patchbay_send_signal_port_disappeared(
178 dbus_uint64_t new_graph_version,
179 dbus_uint64_t client_id,
180 const char * client_name,
181 dbus_uint64_t port_id,
182 const char * port_name)
185 jack_dbus_send_signal(
186 JACK_CONTROLLER_OBJECT_PATH,
187 JACK_DBUS_IFACE_NAME,
188 "PortDisappeared",
189 DBUS_TYPE_UINT64,
190 &new_graph_version,
191 DBUS_TYPE_UINT64,
192 &client_id,
193 DBUS_TYPE_STRING,
194 &client_name,
195 DBUS_TYPE_UINT64,
196 &port_id,
197 DBUS_TYPE_STRING,
198 &port_name,
199 DBUS_TYPE_INVALID);
202 void
203 jack_controller_patchbay_send_signal_ports_connected(
204 dbus_uint64_t new_graph_version,
205 dbus_uint64_t client1_id,
206 const char * client1_name,
207 dbus_uint64_t port1_id,
208 const char * port1_name,
209 dbus_uint64_t client2_id,
210 const char * client2_name,
211 dbus_uint64_t port2_id,
212 const char * port2_name,
213 dbus_uint64_t connection_id)
216 jack_dbus_send_signal(
217 JACK_CONTROLLER_OBJECT_PATH,
218 JACK_DBUS_IFACE_NAME,
219 "PortsConnected",
220 DBUS_TYPE_UINT64,
221 &new_graph_version,
222 DBUS_TYPE_UINT64,
223 &client1_id,
224 DBUS_TYPE_STRING,
225 &client1_name,
226 DBUS_TYPE_UINT64,
227 &port1_id,
228 DBUS_TYPE_STRING,
229 &port1_name,
230 DBUS_TYPE_UINT64,
231 &client2_id,
232 DBUS_TYPE_STRING,
233 &client2_name,
234 DBUS_TYPE_UINT64,
235 &port2_id,
236 DBUS_TYPE_STRING,
237 &port2_name,
238 DBUS_TYPE_UINT64,
239 &connection_id,
240 DBUS_TYPE_INVALID);
243 void
244 jack_controller_patchbay_send_signal_ports_disconnected(
245 dbus_uint64_t new_graph_version,
246 dbus_uint64_t client1_id,
247 const char * client1_name,
248 dbus_uint64_t port1_id,
249 const char * port1_name,
250 dbus_uint64_t client2_id,
251 const char * client2_name,
252 dbus_uint64_t port2_id,
253 const char * port2_name,
254 dbus_uint64_t connection_id)
257 jack_dbus_send_signal(
258 JACK_CONTROLLER_OBJECT_PATH,
259 JACK_DBUS_IFACE_NAME,
260 "PortsDisconnected",
261 DBUS_TYPE_UINT64,
262 &new_graph_version,
263 DBUS_TYPE_UINT64,
264 &client1_id,
265 DBUS_TYPE_STRING,
266 &client1_name,
267 DBUS_TYPE_UINT64,
268 &port1_id,
269 DBUS_TYPE_STRING,
270 &port1_name,
271 DBUS_TYPE_UINT64,
272 &client2_id,
273 DBUS_TYPE_STRING,
274 &client2_name,
275 DBUS_TYPE_UINT64,
276 &port2_id,
277 DBUS_TYPE_STRING,
278 &port2_name,
279 DBUS_TYPE_UINT64,
280 &connection_id,
281 DBUS_TYPE_INVALID);
284 static
285 struct jack_graph_client *
286 jack_controller_patchbay_find_client(
287 struct jack_controller_patchbay *patchbay_ptr,
288 const char *client_name, /* not '\0' terminated */
289 size_t client_name_len) /* without terminating '\0' */
291 struct list_head *node_ptr;
292 struct jack_graph_client *client_ptr;
294 list_for_each(node_ptr, &patchbay_ptr->graph.clients)
296 client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
297 if (strncmp(client_ptr->name, client_name, client_name_len) == 0)
299 return client_ptr;
303 return NULL;
306 static
307 struct jack_graph_client *
308 jack_controller_patchbay_find_client_by_id(
309 struct jack_controller_patchbay *patchbay_ptr,
310 uint64_t id)
312 struct list_head *node_ptr;
313 struct jack_graph_client *client_ptr;
315 list_for_each(node_ptr, &patchbay_ptr->graph.clients)
317 client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
318 if (client_ptr->id == id)
320 return client_ptr;
324 return NULL;
327 static
328 struct jack_graph_client *
329 jack_controller_patchbay_create_client(
330 struct jack_controller_patchbay *patchbay_ptr,
331 const char *client_name, /* not '\0' terminated */
332 size_t client_name_len) /* without terminating '\0' */
334 struct jack_graph_client * client_ptr;
336 client_ptr = malloc(sizeof(struct jack_graph_client));
337 if (client_ptr == NULL)
339 jack_error("Memory allocation of jack_graph_client structure failed.");
340 goto fail;
343 client_ptr->name = malloc(client_name_len + 1);
344 if (client_ptr->name == NULL)
346 jack_error("malloc() failed to allocate memory for client name.");
347 goto fail_free_client;
350 memcpy(client_ptr->name, client_name, client_name_len);
351 client_ptr->name[client_name_len] = 0;
353 client_ptr->pid = jack_get_client_pid(client_ptr->name);
354 jack_info("New client '%s' with PID %d", client_ptr->name, client_ptr->pid);
356 client_ptr->id = patchbay_ptr->next_client_id++;
357 INIT_LIST_HEAD(&client_ptr->ports);
360 pthread_mutex_lock(&patchbay_ptr->lock);
361 list_add_tail(&client_ptr->siblings, &patchbay_ptr->graph.clients);
362 patchbay_ptr->graph.version++;
363 jack_controller_patchbay_send_signal_client_appeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
364 jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
365 pthread_mutex_unlock(&patchbay_ptr->lock);
367 return client_ptr;
369 fail_free_client:
370 free(client_ptr);
372 fail:
373 return NULL;
376 static
377 void
378 jack_controller_patchbay_destroy_client(
379 struct jack_controller_patchbay *patchbay_ptr,
380 struct jack_graph_client *client_ptr)
382 jack_info("Client '%s' with PID %d is out", client_ptr->name, client_ptr->pid);
384 pthread_mutex_lock(&patchbay_ptr->lock);
385 list_del(&client_ptr->siblings);
386 patchbay_ptr->graph.version++;
387 jack_controller_patchbay_send_signal_client_disappeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
388 jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
389 pthread_mutex_unlock(&patchbay_ptr->lock);
391 free(client_ptr->name);
392 free(client_ptr);
395 static
396 void
397 jack_controller_patchbay_destroy_client_by_name(
398 struct jack_controller_patchbay *patchbay_ptr,
399 const char *client_name) /* '\0' terminated */
401 struct jack_graph_client *client_ptr;
403 client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
404 if (client_ptr == NULL)
406 jack_error("Cannot destroy unknown client '%s'", client_name);
407 return;
410 jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
413 static
414 void
415 jack_controller_patchbay_new_port(
416 struct jack_controller_patchbay *patchbay_ptr,
417 const char *port_full_name,
418 uint32_t port_flags,
419 uint32_t port_type)
421 struct jack_graph_client *client_ptr;
422 struct jack_graph_port *port_ptr;
423 const char *port_short_name;
424 size_t client_name_len;
426 //jack_info("name: %s", port_full_name);
428 port_short_name = strchr(port_full_name, ':');
429 if (port_short_name == NULL)
431 jack_error("port name '%s' does not contain ':' separator char", port_full_name);
432 return;
435 port_short_name++; /* skip ':' separator char */
437 client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
439 client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
440 if (client_ptr == NULL)
442 client_ptr = jack_controller_patchbay_create_client(patchbay_ptr, port_full_name, client_name_len);
443 if (client_ptr == NULL)
445 jack_error("Creation of new jack_graph client failed.");
446 return;
450 port_ptr = malloc(sizeof(struct jack_graph_port));
451 if (port_ptr == NULL)
453 jack_error("Memory allocation of jack_graph_port structure failed.");
454 return;
457 port_ptr->name = strdup(port_short_name);
458 if (port_ptr->name == NULL)
460 jack_error("strdup() call for port name '%s' failed.", port_short_name);
461 free(port_ptr);
462 return;
465 port_ptr->id = patchbay_ptr->next_port_id++;
466 port_ptr->flags = port_flags;
467 port_ptr->type = port_type;
468 port_ptr->client = client_ptr;
470 pthread_mutex_lock(&patchbay_ptr->lock);
471 list_add_tail(&port_ptr->siblings_client, &client_ptr->ports);
472 list_add_tail(&port_ptr->siblings_graph, &patchbay_ptr->graph.ports);
473 patchbay_ptr->graph.version++;
474 jack_controller_patchbay_send_signal_port_appeared(
475 patchbay_ptr->graph.version,
476 client_ptr->id,
477 client_ptr->name,
478 port_ptr->id,
479 port_ptr->name,
480 port_ptr->flags,
481 port_ptr->type);
482 jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
483 pthread_mutex_unlock(&patchbay_ptr->lock);
486 static
487 void
488 jack_controller_patchbay_remove_port(
489 struct jack_controller_patchbay *patchbay_ptr,
490 struct jack_graph_port *port_ptr)
492 pthread_mutex_lock(&patchbay_ptr->lock);
493 list_del(&port_ptr->siblings_client);
494 list_del(&port_ptr->siblings_graph);
495 patchbay_ptr->graph.version++;
496 jack_controller_patchbay_send_signal_port_disappeared(patchbay_ptr->graph.version, port_ptr->client->id, port_ptr->client->name, port_ptr->id, port_ptr->name);
497 jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
498 pthread_mutex_unlock(&patchbay_ptr->lock);
500 free(port_ptr->name);
501 free(port_ptr);
504 static
505 struct jack_graph_port *
506 jack_controller_patchbay_find_port_by_id(
507 struct jack_controller_patchbay *patchbay_ptr,
508 uint64_t port_id)
510 struct list_head *node_ptr;
511 struct jack_graph_port *port_ptr;
513 list_for_each(node_ptr, &patchbay_ptr->graph.ports)
515 port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_graph);
516 if (port_ptr->id == port_id)
518 return port_ptr;
522 return NULL;
525 static
526 struct jack_graph_port *
527 jack_controller_patchbay_find_client_port_by_name(
528 struct jack_controller_patchbay *patchbay_ptr,
529 struct jack_graph_client *client_ptr,
530 const char *port_name)
532 struct list_head *node_ptr;
533 struct jack_graph_port *port_ptr;
535 list_for_each(node_ptr, &client_ptr->ports)
537 port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_client);
538 if (strcmp(port_ptr->name, port_name) == 0)
540 return port_ptr;
544 return NULL;
547 static
548 struct jack_graph_port *
549 jack_controller_patchbay_find_port_by_full_name(
550 struct jack_controller_patchbay *patchbay_ptr,
551 const char *port_full_name)
553 const char *port_short_name;
554 size_t client_name_len;
555 struct jack_graph_client *client_ptr;
557 //jack_info("name: %s", port_full_name);
559 port_short_name = strchr(port_full_name, ':');
560 if (port_short_name == NULL)
562 jack_error("port name '%s' does not contain ':' separator char", port_full_name);
563 return NULL;
566 port_short_name++; /* skip ':' separator char */
568 client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
570 client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
571 if (client_ptr == NULL)
573 jack_error("cannot find client of port '%s'", port_full_name);
574 return NULL;
577 return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_short_name);
580 static
581 struct jack_graph_port *
582 jack_controller_patchbay_find_port_by_names(
583 struct jack_controller_patchbay *patchbay_ptr,
584 const char *client_name,
585 const char *port_name)
587 struct jack_graph_client *client_ptr;
589 client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
590 if (client_ptr == NULL)
592 jack_error("cannot find client '%s'", client_name);
593 return NULL;
596 return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_name);
599 static
600 struct jack_graph_connection *
601 jack_controller_patchbay_create_connection(
602 struct jack_controller_patchbay *patchbay_ptr,
603 struct jack_graph_port *port1_ptr,
604 struct jack_graph_port *port2_ptr)
606 struct jack_graph_connection * connection_ptr;
608 connection_ptr = malloc(sizeof(struct jack_graph_connection));
609 if (connection_ptr == NULL)
611 jack_error("Memory allocation of jack_graph_connection structure failed.");
612 return NULL;
615 connection_ptr->id = patchbay_ptr->next_connection_id++;
616 connection_ptr->port1 = port1_ptr;
617 connection_ptr->port2 = port2_ptr;
619 pthread_mutex_lock(&patchbay_ptr->lock);
620 list_add_tail(&connection_ptr->siblings, &patchbay_ptr->graph.connections);
621 patchbay_ptr->graph.version++;
622 jack_controller_patchbay_send_signal_ports_connected(
623 patchbay_ptr->graph.version,
624 port1_ptr->client->id,
625 port1_ptr->client->name,
626 port1_ptr->id,
627 port1_ptr->name,
628 port2_ptr->client->id,
629 port2_ptr->client->name,
630 port2_ptr->id,
631 port2_ptr->name,
632 connection_ptr->id);
633 jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
634 pthread_mutex_unlock(&patchbay_ptr->lock);
636 return connection_ptr;
639 static
640 void
641 jack_controller_patchbay_destroy_connection(
642 struct jack_controller_patchbay *patchbay_ptr,
643 struct jack_graph_connection *connection_ptr)
645 pthread_mutex_lock(&patchbay_ptr->lock);
646 list_del(&connection_ptr->siblings);
647 patchbay_ptr->graph.version++;
648 jack_controller_patchbay_send_signal_ports_disconnected(
649 patchbay_ptr->graph.version,
650 connection_ptr->port1->client->id,
651 connection_ptr->port1->client->name,
652 connection_ptr->port1->id,
653 connection_ptr->port1->name,
654 connection_ptr->port2->client->id,
655 connection_ptr->port2->client->name,
656 connection_ptr->port2->id,
657 connection_ptr->port2->name,
658 connection_ptr->id);
659 jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
660 pthread_mutex_unlock(&patchbay_ptr->lock);
662 free(connection_ptr);
665 static
666 struct jack_graph_connection *
667 jack_controller_patchbay_find_connection(
668 struct jack_controller_patchbay *patchbay_ptr,
669 struct jack_graph_port *port1_ptr,
670 struct jack_graph_port *port2_ptr)
672 struct list_head *node_ptr;
673 struct jack_graph_connection *connection_ptr;
675 list_for_each(node_ptr, &patchbay_ptr->graph.connections)
677 connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
678 if ((connection_ptr->port1 == port1_ptr &&
679 connection_ptr->port2 == port2_ptr) ||
680 (connection_ptr->port1 == port2_ptr &&
681 connection_ptr->port2 == port1_ptr))
683 return connection_ptr;
687 return NULL;
690 static
691 struct jack_graph_connection *
692 jack_controller_patchbay_find_connection_by_id(
693 struct jack_controller_patchbay *patchbay_ptr,
694 uint64_t connection_id)
696 struct list_head *node_ptr;
697 struct jack_graph_connection *connection_ptr;
699 list_for_each(node_ptr, &patchbay_ptr->graph.connections)
701 connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
702 if (connection_ptr->id == connection_id)
704 return connection_ptr;
708 return NULL;
711 static
712 bool
713 jack_controller_patchbay_connect(
714 struct jack_dbus_method_call *dbus_call_ptr,
715 struct jack_controller *controller_ptr,
716 struct jack_graph_port *port1_ptr,
717 struct jack_graph_port *port2_ptr)
719 int ret;
720 char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
721 char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
723 sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
724 sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
726 ret = jack_connect(controller_ptr->client, port1_name, port2_name);
727 if (ret != 0)
729 jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
730 return false;
733 return true;
736 static
737 bool
738 jack_controller_patchbay_disconnect(
739 struct jack_dbus_method_call *dbus_call_ptr,
740 struct jack_controller *controller_ptr,
741 struct jack_graph_port *port1_ptr,
742 struct jack_graph_port *port2_ptr)
744 int ret;
745 char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
746 char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
748 sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
749 sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
751 ret = jack_disconnect(controller_ptr->client, port1_name, port2_name);
752 if (ret != 0)
754 jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
755 return false;
758 return true;
761 #define controller_ptr ((struct jack_controller *)call->context)
762 #define patchbay_ptr ((struct jack_controller_patchbay *)controller_ptr->patchbay_context)
764 static
765 void
766 jack_controller_dbus_get_all_ports(
767 struct jack_dbus_method_call * call)
769 struct list_head * client_node_ptr;
770 struct list_head * port_node_ptr;
771 struct jack_graph_client * client_ptr;
772 struct jack_graph_port * port_ptr;
773 DBusMessageIter iter, sub_iter;
774 char fullname[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
775 char *fullname_var = fullname;
777 if (!controller_ptr->started)
779 jack_dbus_error(
780 call,
781 JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
782 "Can't execute this method with stopped JACK server");
783 return;
786 call->reply = dbus_message_new_method_return (call->message);
787 if (!call->reply)
789 goto fail;
792 dbus_message_iter_init_append (call->reply, &iter);
794 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
796 goto fail_unref;
799 pthread_mutex_lock(&patchbay_ptr->lock);
801 list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
803 client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
805 list_for_each(port_node_ptr, &client_ptr->ports)
807 port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
809 jack_info("%s:%s", client_ptr->name, port_ptr->name);
810 sprintf(fullname, "%s:%s", client_ptr->name, port_ptr->name);
811 if (!dbus_message_iter_append_basic (&sub_iter, DBUS_TYPE_STRING, &fullname_var))
813 pthread_mutex_unlock(&patchbay_ptr->lock);
814 dbus_message_iter_close_container (&iter, &sub_iter);
815 goto fail_unref;
820 pthread_mutex_unlock(&patchbay_ptr->lock);
822 if (!dbus_message_iter_close_container (&iter, &sub_iter))
824 goto fail_unref;
827 return;
829 fail_unref:
830 dbus_message_unref (call->reply);
831 call->reply = NULL;
833 fail:
834 jack_error ("Ran out of memory trying to construct method return");
837 static
838 void
839 jack_controller_dbus_get_graph(
840 struct jack_dbus_method_call * call)
842 struct list_head * client_node_ptr;
843 struct list_head * port_node_ptr;
844 struct list_head * connection_node_ptr;
845 struct jack_graph_client * client_ptr;
846 struct jack_graph_port * port_ptr;
847 struct jack_graph_connection * connection_ptr;
848 DBusMessageIter iter;
849 DBusMessageIter clients_array_iter;
850 DBusMessageIter client_struct_iter;
851 DBusMessageIter ports_array_iter;
852 DBusMessageIter port_struct_iter;
853 dbus_uint64_t version;
854 DBusMessageIter connections_array_iter;
855 DBusMessageIter connection_struct_iter;
857 if (!controller_ptr->started)
859 jack_dbus_error(
860 call,
861 JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
862 "Can't execute this method with stopped JACK server");
863 return;
866 if (!jack_dbus_get_method_args(call, DBUS_TYPE_UINT64, &version, DBUS_TYPE_INVALID))
868 /* The method call had invalid arguments meaning that
869 * jack_dbus_get_method_args() has constructed an error for us.
871 goto exit;
874 //jack_info("Getting graph, know version is %" PRIu32, version);
876 call->reply = dbus_message_new_method_return(call->message);
877 if (!call->reply)
879 jack_error("Ran out of memory trying to construct method return");
880 goto exit;
883 dbus_message_iter_init_append (call->reply, &iter);
885 pthread_mutex_lock(&patchbay_ptr->lock);
887 if (version > patchbay_ptr->graph.version)
889 jack_dbus_error(
890 call,
891 JACK_DBUS_ERROR_INVALID_ARGS,
892 "known graph version %" PRIu64 " is newer than actual version %" PRIu64,
893 version,
894 patchbay_ptr->graph.version);
895 pthread_mutex_unlock(&patchbay_ptr->lock);
896 goto exit;
899 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &patchbay_ptr->graph.version))
901 goto nomem_unlock;
904 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tsa(tsuu))", &clients_array_iter))
906 goto nomem_unlock;
909 if (version < patchbay_ptr->graph.version)
911 list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
913 client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
915 if (!dbus_message_iter_open_container (&clients_array_iter, DBUS_TYPE_STRUCT, NULL, &client_struct_iter))
917 goto nomem_close_clients_array;
920 if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_UINT64, &client_ptr->id))
922 goto nomem_close_client_struct;
925 if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_STRING, &client_ptr->name))
927 goto nomem_close_client_struct;
930 if (!dbus_message_iter_open_container(&client_struct_iter, DBUS_TYPE_ARRAY, "(tsuu)", &ports_array_iter))
932 goto nomem_close_client_struct;
935 list_for_each(port_node_ptr, &client_ptr->ports)
937 port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
939 if (!dbus_message_iter_open_container(&ports_array_iter, DBUS_TYPE_STRUCT, NULL, &port_struct_iter))
941 goto nomem_close_ports_array;
944 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT64, &port_ptr->id))
946 goto nomem_close_port_struct;
949 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_STRING, &port_ptr->name))
951 goto nomem_close_port_struct;
954 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->flags))
956 goto nomem_close_port_struct;
959 if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->type))
961 goto nomem_close_port_struct;
964 if (!dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter))
966 goto nomem_close_ports_array;
970 if (!dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter))
972 goto nomem_close_client_struct;
975 if (!dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter))
977 goto nomem_close_clients_array;
982 if (!dbus_message_iter_close_container(&iter, &clients_array_iter))
984 goto nomem_unlock;
987 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tstststst)", &connections_array_iter))
989 goto nomem_unlock;
992 if (version < patchbay_ptr->graph.version)
994 list_for_each(connection_node_ptr, &patchbay_ptr->graph.connections)
996 connection_ptr = list_entry(connection_node_ptr, struct jack_graph_connection, siblings);
998 if (!dbus_message_iter_open_container(&connections_array_iter, DBUS_TYPE_STRUCT, NULL, &connection_struct_iter))
1000 goto nomem_close_connections_array;
1003 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->client->id))
1005 goto nomem_close_connection_struct;
1008 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->client->name))
1010 goto nomem_close_connection_struct;
1013 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->id))
1015 goto nomem_close_connection_struct;
1018 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->name))
1020 goto nomem_close_connection_struct;
1023 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->client->id))
1025 goto nomem_close_connection_struct;
1028 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->client->name))
1030 goto nomem_close_connection_struct;
1033 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->id))
1035 goto nomem_close_connection_struct;
1038 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->name))
1040 goto nomem_close_connection_struct;
1043 if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->id))
1045 goto nomem_close_connection_struct;
1048 if (!dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter))
1050 goto nomem_close_connections_array;
1055 if (!dbus_message_iter_close_container(&iter, &connections_array_iter))
1057 goto nomem_unlock;
1060 pthread_mutex_unlock(&patchbay_ptr->lock);
1062 return;
1064 nomem_close_connection_struct:
1065 dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter);
1067 nomem_close_connections_array:
1068 dbus_message_iter_close_container(&iter, &connections_array_iter);
1069 goto nomem_unlock;
1071 nomem_close_port_struct:
1072 dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter);
1074 nomem_close_ports_array:
1075 dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter);
1077 nomem_close_client_struct:
1078 dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter);
1080 nomem_close_clients_array:
1081 dbus_message_iter_close_container(&iter, &clients_array_iter);
1083 nomem_unlock:
1084 pthread_mutex_unlock(&patchbay_ptr->lock);
1086 //nomem:
1087 dbus_message_unref(call->reply);
1088 call->reply = NULL;
1089 jack_error("Ran out of memory trying to construct method return");
1091 exit:
1092 return;
1095 static
1096 void
1097 jack_controller_dbus_connect_ports_by_name(
1098 struct jack_dbus_method_call * call)
1100 const char * client1_name;
1101 const char * port1_name;
1102 const char * client2_name;
1103 const char * port2_name;
1104 struct jack_graph_port *port1_ptr;
1105 struct jack_graph_port *port2_ptr;
1107 /* jack_info("jack_controller_dbus_connect_ports_by_name() called."); */
1109 if (!controller_ptr->started)
1111 jack_dbus_error(
1112 call,
1113 JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
1114 "Can't execute this method with stopped JACK server");
1115 return;
1118 if (!jack_dbus_get_method_args(
1119 call,
1120 DBUS_TYPE_STRING,
1121 &client1_name,
1122 DBUS_TYPE_STRING,
1123 &port1_name,
1124 DBUS_TYPE_STRING,
1125 &client2_name,
1126 DBUS_TYPE_STRING,
1127 &port2_name,
1128 DBUS_TYPE_INVALID))
1130 /* The method call had invalid arguments meaning that
1131 * jack_dbus_get_method_args() has constructed an error for us.
1133 return;
1136 /* jack_info("connecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
1138 pthread_mutex_lock(&patchbay_ptr->lock);
1140 port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
1141 if (port1_ptr == NULL)
1143 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
1144 goto unlock;
1147 port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
1148 if (port2_ptr == NULL)
1150 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
1151 goto unlock;
1154 if (!jack_controller_patchbay_connect(
1155 call,
1156 controller_ptr,
1157 port1_ptr,
1158 port2_ptr))
1160 /* jack_controller_patchbay_connect() constructed error reply */
1161 goto unlock;
1164 jack_dbus_construct_method_return_empty(call);
1166 unlock:
1167 pthread_mutex_unlock(&patchbay_ptr->lock);
1170 static
1171 void
1172 jack_controller_dbus_connect_ports_by_id(
1173 struct jack_dbus_method_call * call)
1175 dbus_uint64_t port1_id;
1176 dbus_uint64_t port2_id;
1177 struct jack_graph_port *port1_ptr;
1178 struct jack_graph_port *port2_ptr;
1180 /* jack_info("jack_controller_dbus_connect_ports_by_id() called."); */
1182 if (!controller_ptr->started)
1184 jack_dbus_error(
1185 call,
1186 JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
1187 "Can't execute this method with stopped JACK server");
1188 return;
1191 if (!jack_dbus_get_method_args(
1192 call,
1193 DBUS_TYPE_UINT64,
1194 &port1_id,
1195 DBUS_TYPE_UINT64,
1196 &port2_id,
1197 DBUS_TYPE_INVALID))
1199 /* The method call had invalid arguments meaning that
1200 * jack_dbus_get_method_args() has constructed an error for us.
1202 return;
1205 pthread_mutex_lock(&patchbay_ptr->lock);
1207 port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
1208 if (port1_ptr == NULL)
1210 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
1211 goto unlock;
1214 port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
1215 if (port2_ptr == NULL)
1217 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
1218 goto unlock;
1221 if (!jack_controller_patchbay_connect(
1222 call,
1223 controller_ptr,
1224 port1_ptr,
1225 port2_ptr))
1227 /* jack_controller_patchbay_connect() constructed error reply */
1228 goto unlock;
1231 jack_dbus_construct_method_return_empty(call);
1233 unlock:
1234 pthread_mutex_unlock(&patchbay_ptr->lock);
1237 static
1238 void
1239 jack_controller_dbus_disconnect_ports_by_name(
1240 struct jack_dbus_method_call * call)
1242 const char * client1_name;
1243 const char * port1_name;
1244 const char * client2_name;
1245 const char * port2_name;
1246 struct jack_graph_port *port1_ptr;
1247 struct jack_graph_port *port2_ptr;
1249 /* jack_info("jack_controller_dbus_disconnect_ports_by_name() called."); */
1251 if (!controller_ptr->started)
1253 jack_dbus_error(
1254 call,
1255 JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
1256 "Can't execute this method with stopped JACK server");
1257 return;
1260 if (!jack_dbus_get_method_args(
1261 call,
1262 DBUS_TYPE_STRING,
1263 &client1_name,
1264 DBUS_TYPE_STRING,
1265 &port1_name,
1266 DBUS_TYPE_STRING,
1267 &client2_name,
1268 DBUS_TYPE_STRING,
1269 &port2_name,
1270 DBUS_TYPE_INVALID))
1272 /* The method call had invalid arguments meaning that
1273 * jack_dbus_get_method_args() has constructed an error for us.
1275 return;
1278 /* jack_info("disconnecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
1280 pthread_mutex_lock(&patchbay_ptr->lock);
1282 port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
1283 if (port1_ptr == NULL)
1285 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
1286 goto unlock;
1289 port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
1290 if (port2_ptr == NULL)
1292 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
1293 goto unlock;
1296 if (!jack_controller_patchbay_disconnect(
1297 call,
1298 controller_ptr,
1299 port1_ptr,
1300 port2_ptr))
1302 /* jack_controller_patchbay_connect() constructed error reply */
1303 goto unlock;
1306 jack_dbus_construct_method_return_empty(call);
1308 unlock:
1309 pthread_mutex_unlock(&patchbay_ptr->lock);
1312 static
1313 void
1314 jack_controller_dbus_disconnect_ports_by_id(
1315 struct jack_dbus_method_call * call)
1317 dbus_uint64_t port1_id;
1318 dbus_uint64_t port2_id;
1319 struct jack_graph_port *port1_ptr;
1320 struct jack_graph_port *port2_ptr;
1322 /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
1324 if (!controller_ptr->started)
1326 jack_dbus_error(
1327 call,
1328 JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
1329 "Can't execute this method with stopped JACK server");
1330 return;
1333 if (!jack_dbus_get_method_args(
1334 call,
1335 DBUS_TYPE_UINT64,
1336 &port1_id,
1337 DBUS_TYPE_UINT64,
1338 &port2_id,
1339 DBUS_TYPE_INVALID))
1341 /* The method call had invalid arguments meaning that
1342 * jack_dbus_get_method_args() has constructed an error for us.
1344 return;
1347 pthread_mutex_lock(&patchbay_ptr->lock);
1349 port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
1350 if (port1_ptr == NULL)
1352 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
1353 return;
1356 port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
1357 if (port2_ptr == NULL)
1359 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
1360 return;
1363 if (!jack_controller_patchbay_disconnect(
1364 call,
1365 controller_ptr,
1366 port1_ptr,
1367 port2_ptr))
1369 /* jack_controller_patchbay_connect() constructed error reply */
1370 goto unlock;
1373 jack_dbus_construct_method_return_empty(call);
1375 unlock:
1376 pthread_mutex_unlock(&patchbay_ptr->lock);
1379 static
1380 void
1381 jack_controller_dbus_disconnect_ports_by_connection_id(
1382 struct jack_dbus_method_call * call)
1384 dbus_uint64_t connection_id;
1385 struct jack_graph_connection *connection_ptr;
1387 /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
1389 if (!jack_dbus_get_method_args(
1390 call,
1391 DBUS_TYPE_UINT64,
1392 &connection_id,
1393 DBUS_TYPE_INVALID))
1395 /* The method call had invalid arguments meaning that
1396 * jack_dbus_get_method_args() has constructed an error for us.
1398 return;
1401 pthread_mutex_lock(&patchbay_ptr->lock);
1403 connection_ptr = jack_controller_patchbay_find_connection_by_id(patchbay_ptr, connection_id);
1404 if (connection_ptr == NULL)
1406 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find connection %" PRIu64, connection_id);
1407 goto unlock;
1410 if (!jack_controller_patchbay_disconnect(
1411 call,
1412 controller_ptr,
1413 connection_ptr->port1,
1414 connection_ptr->port2))
1416 /* jack_controller_patchbay_connect() constructed error reply */
1417 goto unlock;
1420 jack_dbus_construct_method_return_empty(call);
1422 unlock:
1423 pthread_mutex_unlock(&patchbay_ptr->lock);
1426 static
1427 void
1428 jack_controller_dbus_get_client_pid(
1429 struct jack_dbus_method_call * call)
1431 dbus_uint64_t client_id;
1432 struct jack_graph_client *client_ptr;
1433 message_arg_t arg;
1435 /* jack_info("jack_controller_dbus_get_client_pid() called."); */
1437 if (!jack_dbus_get_method_args(
1438 call,
1439 DBUS_TYPE_UINT64,
1440 &client_id,
1441 DBUS_TYPE_INVALID))
1443 /* The method call had invalid arguments meaning that
1444 * jack_dbus_get_method_args() has constructed an error for us.
1446 return;
1449 pthread_mutex_lock(&patchbay_ptr->lock);
1451 client_ptr = jack_controller_patchbay_find_client_by_id(patchbay_ptr, client_id);
1452 if (client_ptr == NULL)
1454 jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find client %" PRIu64, client_id);
1455 goto unlock;
1458 arg.int64 = client_ptr->pid;
1460 jack_dbus_construct_method_return_single(call, DBUS_TYPE_INT64, arg);
1462 unlock:
1463 pthread_mutex_unlock(&patchbay_ptr->lock);
1466 #undef controller_ptr
1467 #define controller_ptr ((struct jack_controller *)context)
1469 static
1471 jack_controller_graph_order_callback(
1472 void *context)
1474 const char **ports;
1475 int i;
1476 jack_port_t *port_ptr;
1478 if (patchbay_ptr->graph.version > 1)
1480 /* we use this only for initial catchup */
1481 return 0;
1484 ports = jack_get_ports(controller_ptr->client, NULL, NULL, 0);
1485 if (ports)
1487 for (i = 0; ports[i]; ++i)
1489 jack_info("graph reorder: new port '%s'", ports[i]);
1490 port_ptr = jack_port_by_name(controller_ptr->client, ports[i]);;
1491 jack_controller_patchbay_new_port(patchbay_ptr, ports[i], jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
1494 free(ports);
1497 if (patchbay_ptr->graph.version == 1)
1499 /* we have empty initial graph, increment graph version,
1500 so we dont do jack_get_ports() again,
1501 on next next graph change */
1502 patchbay_ptr->graph.version++;
1505 return 0;
1508 void
1509 jack_controller_client_registration_callback(
1510 const char *client_name,
1511 int created,
1512 void *context)
1514 if (created)
1516 jack_log("client '%s' created", client_name);
1517 jack_controller_patchbay_create_client(patchbay_ptr, client_name, strlen(client_name));
1519 else
1521 jack_log("client '%s' destroyed", client_name);
1522 jack_controller_patchbay_destroy_client_by_name(patchbay_ptr, client_name);
1526 void
1527 jack_controller_port_registration_callback(
1528 jack_port_id_t port_id,
1529 int created,
1530 void *context)
1532 jack_port_t *port_ptr;
1533 struct jack_graph_port *graph_port_ptr;
1534 const char *port_name;
1536 port_ptr = jack_port_by_id(controller_ptr->client, port_id);
1537 port_name = jack_port_name(port_ptr);
1539 if (created)
1541 jack_log("port '%s' created", port_name);
1542 jack_controller_patchbay_new_port(patchbay_ptr, port_name, jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
1544 else
1546 jack_log("port '%s' destroyed", port_name);
1547 graph_port_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port_name);
1548 if (graph_port_ptr == NULL)
1550 jack_error("Failed to find port '%s' to destroy", port_name);
1551 return;
1554 jack_controller_patchbay_remove_port(patchbay_ptr, graph_port_ptr);
1558 void
1559 jack_controller_port_connect_callback(
1560 jack_port_id_t port1_id,
1561 jack_port_id_t port2_id,
1562 int connect,
1563 void *context)
1565 jack_port_t *port1;
1566 jack_port_t *port2;
1567 const char *port1_name;
1568 const char *port2_name;
1569 struct jack_graph_port *port1_ptr;
1570 struct jack_graph_port *port2_ptr;
1571 struct jack_graph_connection *connection_ptr;
1573 port1 = jack_port_by_id(controller_ptr->client, port1_id);
1574 port2 = jack_port_by_id(controller_ptr->client, port2_id);
1576 port1_name = jack_port_name(port1);
1577 port2_name = jack_port_name(port2);
1579 port1_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port1_name);
1580 if (port1_ptr == NULL)
1582 jack_error("Failed to find port '%s' to [dis]connect", port1_name);
1583 return;
1586 port2_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port2_name);
1587 if (port2_ptr == NULL)
1589 jack_error("Failed to find port '%s' to [dis]connect", port2_name);
1590 return;
1593 if (connect)
1595 jack_info("Connecting '%s' to '%s'", port1_name, port2_name);
1596 connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
1597 if (connection_ptr != NULL)
1599 jack_error("'%s' and '%s' are already connected", port1_name, port2_name);
1600 return;
1603 jack_controller_patchbay_create_connection(patchbay_ptr, port1_ptr, port2_ptr);
1605 else
1607 jack_info("Disonnecting '%s' from '%s'", port1_name, port2_name);
1608 connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
1609 if (connection_ptr == NULL)
1611 jack_error("Cannot find connection being removed");
1612 return;
1615 jack_controller_patchbay_destroy_connection(patchbay_ptr, connection_ptr);
1619 #undef controller_ptr
1621 void
1622 jack_controller_patchbay_uninit(
1623 struct jack_controller * controller_ptr)
1625 struct jack_graph_client *client_ptr;
1626 struct jack_graph_port *port_ptr;
1628 /* jack_info("jack_controller_patchbay_uninit() called"); */
1630 while (!list_empty(&patchbay_ptr->graph.ports))
1632 port_ptr = list_entry(patchbay_ptr->graph.ports.next, struct jack_graph_port, siblings_graph);
1633 jack_controller_patchbay_remove_port(patchbay_ptr, port_ptr);
1636 while (!list_empty(&patchbay_ptr->graph.clients))
1638 client_ptr = list_entry(patchbay_ptr->graph.clients.next, struct jack_graph_client, siblings);
1639 jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
1642 pthread_mutex_destroy(&patchbay_ptr->lock);
1645 #undef patchbay_ptr
1647 bool
1648 jack_controller_patchbay_init(
1649 struct jack_controller * controller_ptr)
1651 int ret;
1652 struct jack_controller_patchbay * patchbay_ptr;
1653 pthread_mutexattr_t attr;
1655 /* jack_info("jack_controller_patchbay_init() called"); */
1657 patchbay_ptr = malloc(sizeof(struct jack_controller_patchbay));
1658 if (patchbay_ptr == NULL)
1660 jack_error("Memory allocation of jack_controller_patchbay structure failed.");
1661 goto fail;
1664 ret = pthread_mutexattr_init(&attr);
1665 if (ret != 0)
1667 goto fail;
1670 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
1671 if (ret != 0)
1673 goto fail;
1676 pthread_mutex_init(&patchbay_ptr->lock, &attr);
1677 INIT_LIST_HEAD(&patchbay_ptr->graph.clients);
1678 INIT_LIST_HEAD(&patchbay_ptr->graph.ports);
1679 INIT_LIST_HEAD(&patchbay_ptr->graph.connections);
1680 patchbay_ptr->graph.version = 1;
1681 patchbay_ptr->next_client_id = 1;
1682 patchbay_ptr->next_port_id = 1;
1683 patchbay_ptr->next_connection_id = 1;
1685 controller_ptr->patchbay_context = patchbay_ptr;
1687 ret = jack_set_graph_order_callback(controller_ptr->client, jack_controller_graph_order_callback, controller_ptr);
1688 if (ret != 0)
1690 jack_error("jack_set_graph_order_callback() failed with error %d", ret);
1691 goto fail_uninit_mutex;
1694 ret = jack_set_client_registration_callback(controller_ptr->client, jack_controller_client_registration_callback, controller_ptr);
1695 if (ret != 0)
1697 jack_error("jack_set_client_registration_callback() failed with error %d", ret);
1698 goto fail_uninit_mutex;
1701 ret = jack_set_port_registration_callback(controller_ptr->client, jack_controller_port_registration_callback, controller_ptr);
1702 if (ret != 0)
1704 jack_error("jack_set_port_registration_callback() failed with error %d", ret);
1705 goto fail_uninit_mutex;
1708 ret = jack_set_port_connect_callback(controller_ptr->client, jack_controller_port_connect_callback, controller_ptr);
1709 if (ret != 0)
1711 jack_error("jack_set_port_connect_callback() failed with error %d", ret);
1712 goto fail_uninit_mutex;
1715 return true;
1717 fail_uninit_mutex:
1718 pthread_mutex_destroy(&patchbay_ptr->lock);
1720 fail:
1721 return false;
1724 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetAllPorts)
1725 JACK_DBUS_METHOD_ARGUMENT("ports_list", "as", true)
1726 JACK_DBUS_METHOD_ARGUMENTS_END
1728 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetGraph)
1729 JACK_DBUS_METHOD_ARGUMENT("known_graph_version", DBUS_TYPE_UINT64_AS_STRING, false)
1730 JACK_DBUS_METHOD_ARGUMENT("current_graph_version", DBUS_TYPE_UINT64_AS_STRING, true)
1731 JACK_DBUS_METHOD_ARGUMENT("clients_and_ports", "a(tsa(tsuu))", true)
1732 JACK_DBUS_METHOD_ARGUMENT("connections", "a(tstststst)", true)
1733 JACK_DBUS_METHOD_ARGUMENTS_END
1735 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByName)
1736 JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
1737 JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
1738 JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
1739 JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
1740 JACK_DBUS_METHOD_ARGUMENTS_END
1742 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByID)
1743 JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
1744 JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
1745 JACK_DBUS_METHOD_ARGUMENTS_END
1747 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByName)
1748 JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
1749 JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
1750 JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
1751 JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
1752 JACK_DBUS_METHOD_ARGUMENTS_END
1754 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByID)
1755 JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
1756 JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
1757 JACK_DBUS_METHOD_ARGUMENTS_END
1759 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByConnectionID)
1760 JACK_DBUS_METHOD_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING, false)
1761 JACK_DBUS_METHOD_ARGUMENTS_END
1763 JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetClientPID)
1764 JACK_DBUS_METHOD_ARGUMENT("client_id", DBUS_TYPE_INT64_AS_STRING, false)
1765 JACK_DBUS_METHOD_ARGUMENTS_END
1767 JACK_DBUS_METHODS_BEGIN
1768 JACK_DBUS_METHOD_DESCRIBE(GetAllPorts, jack_controller_dbus_get_all_ports)
1769 JACK_DBUS_METHOD_DESCRIBE(GetGraph, jack_controller_dbus_get_graph)
1770 JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByName, jack_controller_dbus_connect_ports_by_name)
1771 JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByID, jack_controller_dbus_connect_ports_by_id)
1772 JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByName, jack_controller_dbus_disconnect_ports_by_name)
1773 JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByID, jack_controller_dbus_disconnect_ports_by_id)
1774 JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByConnectionID, jack_controller_dbus_disconnect_ports_by_connection_id)
1775 JACK_DBUS_METHOD_DESCRIBE(GetClientPID, jack_controller_dbus_get_client_pid)
1776 JACK_DBUS_METHODS_END
1778 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(GraphChanged)
1779 JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
1780 JACK_DBUS_SIGNAL_ARGUMENTS_END
1782 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientAppeared)
1783 JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
1784 JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
1785 JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
1786 JACK_DBUS_SIGNAL_ARGUMENTS_END
1788 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientDisappeared)
1789 JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
1790 JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
1791 JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
1792 JACK_DBUS_SIGNAL_ARGUMENTS_END
1794 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortAppeared)
1795 JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
1796 JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
1797 JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
1798 JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
1799 JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
1800 JACK_DBUS_SIGNAL_ARGUMENT("port_flags", DBUS_TYPE_UINT32_AS_STRING)
1801 JACK_DBUS_SIGNAL_ARGUMENT("port_type", DBUS_TYPE_UINT32_AS_STRING)
1802 JACK_DBUS_SIGNAL_ARGUMENTS_END
1804 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortDisappeared)
1805 JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
1806 JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
1807 JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
1808 JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
1809 JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
1810 JACK_DBUS_SIGNAL_ARGUMENTS_END
1812 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsConnected)
1813 JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
1814 JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
1815 JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
1816 JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
1817 JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
1818 JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
1819 JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
1820 JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
1821 JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
1822 JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
1823 JACK_DBUS_SIGNAL_ARGUMENTS_END
1825 JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsDisconnected)
1826 JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
1827 JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
1828 JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
1829 JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
1830 JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
1831 JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
1832 JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
1833 JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
1834 JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
1835 JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
1836 JACK_DBUS_SIGNAL_ARGUMENTS_END
1838 JACK_DBUS_SIGNALS_BEGIN
1839 JACK_DBUS_SIGNAL_DESCRIBE(GraphChanged)
1840 JACK_DBUS_SIGNAL_DESCRIBE(ClientAppeared)
1841 JACK_DBUS_SIGNAL_DESCRIBE(ClientDisappeared)
1842 JACK_DBUS_SIGNAL_DESCRIBE(PortAppeared)
1843 JACK_DBUS_SIGNAL_DESCRIBE(PortDisappeared)
1844 JACK_DBUS_SIGNAL_DESCRIBE(PortsConnected)
1845 JACK_DBUS_SIGNAL_DESCRIBE(PortsDisconnected)
1846 JACK_DBUS_SIGNALS_END
1848 JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_patchbay, JACK_DBUS_IFACE_NAME)
1849 JACK_DBUS_IFACE_EXPOSE_METHODS
1850 JACK_DBUS_IFACE_EXPOSE_SIGNALS
1851 JACK_DBUS_IFACE_END