jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7381 lines
220KB

  1. /* -*- Mode: C ; c-basic-offset: 4 -*- */
  2. /*
  3. Copyright (C) 2008 Nedko Arnaudov
  4. Copyright (C) 2008 Juuso Alasuutari
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #define _GNU_SOURCE /* PTHREAD_MUTEX_RECURSIVE */
  17. #include <stdint.h>
  18. #include <inttypes.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <dbus/dbus.h>
  23. #include <pthread.h>
  24. #include "jackdbus.h"
  25. #include "controller_internal.h"
  26. #include "list.h"
  27. #define JACK_DBUS_IFACE_NAME "org.jackaudio.JackPatchbay"
  28. /* FIXME: these need to be retrieved from common headers */
  29. #define JACK_CLIENT_NAME_SIZE 64
  30. #define JACK_PORT_NAME_SIZE 256
  31. struct jack_graph
  32. {
  33. uint64_t version;
  34. struct list_head clients;
  35. struct list_head ports;
  36. struct list_head connections;
  37. };
  38. struct jack_graph_client
  39. {
  40. uint64_t id;
  41. char * name;
  42. int pid;
  43. struct list_head siblings;
  44. struct list_head ports;
  45. };
  46. struct jack_graph_port
  47. {
  48. uint64_t id;
  49. char * name;
  50. uint32_t flags;
  51. uint32_t type;
  52. struct list_head siblings_graph;
  53. struct list_head siblings_client;
  54. struct jack_graph_client * client;
  55. };
  56. struct jack_graph_connection
  57. {
  58. uint64_t id;
  59. struct jack_graph_port * port1;
  60. struct jack_graph_port * port2;
  61. struct list_head siblings;
  62. };
  63. struct jack_controller_patchbay
  64. {
  65. pthread_mutex_t lock;
  66. struct jack_graph graph;
  67. uint64_t next_client_id;
  68. uint64_t next_port_id;
  69. uint64_t next_connection_id;
  70. };
  71. void
  72. jack_controller_patchbay_send_signal_graph_changed(
  73. dbus_uint64_t new_graph_version)
  74. {
  75. jack_dbus_send_signal(
  76. JACK_CONTROLLER_OBJECT_PATH,
  77. JACK_DBUS_IFACE_NAME,
  78. "GraphChanged",
  79. DBUS_TYPE_UINT64,
  80. &new_graph_version,
  81. DBUS_TYPE_INVALID);
  82. }
  83. void
  84. jack_controller_patchbay_send_signal_client_appeared(
  85. dbus_uint64_t new_graph_version,
  86. dbus_uint64_t client_id,
  87. const char * client_name)
  88. {
  89. jack_dbus_send_signal(
  90. JACK_CONTROLLER_OBJECT_PATH,
  91. JACK_DBUS_IFACE_NAME,
  92. "ClientAppeared",
  93. DBUS_TYPE_UINT64,
  94. &new_graph_version,
  95. DBUS_TYPE_UINT64,
  96. &client_id,
  97. DBUS_TYPE_STRING,
  98. &client_name,
  99. DBUS_TYPE_INVALID);
  100. }
  101. void
  102. jack_controller_patchbay_send_signal_client_disappeared(
  103. dbus_uint64_t new_graph_version,
  104. dbus_uint64_t client_id,
  105. const char * client_name)
  106. {
  107. jack_dbus_send_signal(
  108. JACK_CONTROLLER_OBJECT_PATH,
  109. JACK_DBUS_IFACE_NAME,
  110. "ClientDisappeared",
  111. DBUS_TYPE_UINT64,
  112. &new_graph_version,
  113. DBUS_TYPE_UINT64,
  114. &client_id,
  115. DBUS_TYPE_STRING,
  116. &client_name,
  117. DBUS_TYPE_INVALID);
  118. }
  119. void
  120. jack_controller_patchbay_send_signal_port_appeared(
  121. dbus_uint64_t new_graph_version,
  122. dbus_uint64_t client_id,
  123. const char * client_name,
  124. dbus_uint64_t port_id,
  125. const char * port_name,
  126. dbus_uint32_t port_flags,
  127. dbus_uint32_t port_type)
  128. {
  129. jack_dbus_send_signal(
  130. JACK_CONTROLLER_OBJECT_PATH,
  131. JACK_DBUS_IFACE_NAME,
  132. "PortAppeared",
  133. DBUS_TYPE_UINT64,
  134. &new_graph_version,
  135. DBUS_TYPE_UINT64,
  136. &client_id,
  137. DBUS_TYPE_STRING,
  138. &client_name,
  139. DBUS_TYPE_UINT64,
  140. &port_id,
  141. DBUS_TYPE_STRING,
  142. &port_name,
  143. DBUS_TYPE_UINT32,
  144. &port_flags,
  145. DBUS_TYPE_UINT32,
  146. &port_type,
  147. DBUS_TYPE_INVALID);
  148. }
  149. void
  150. jack_controller_patchbay_send_signal_port_disappeared(
  151. dbus_uint64_t new_graph_version,
  152. dbus_uint64_t client_id,
  153. const char * client_name,
  154. dbus_uint64_t port_id,
  155. const char * port_name)
  156. {
  157. jack_dbus_send_signal(
  158. JACK_CONTROLLER_OBJECT_PATH,
  159. JACK_DBUS_IFACE_NAME,
  160. "PortDisappeared",
  161. DBUS_TYPE_UINT64,
  162. &new_graph_version,
  163. DBUS_TYPE_UINT64,
  164. &client_id,
  165. DBUS_TYPE_STRING,
  166. &client_name,
  167. DBUS_TYPE_UINT64,
  168. &port_id,
  169. DBUS_TYPE_STRING,
  170. &port_name,
  171. DBUS_TYPE_INVALID);
  172. }
  173. void
  174. jack_controller_patchbay_send_signal_ports_connected(
  175. dbus_uint64_t new_graph_version,
  176. dbus_uint64_t client1_id,
  177. const char * client1_name,
  178. dbus_uint64_t port1_id,
  179. const char * port1_name,
  180. dbus_uint64_t client2_id,
  181. const char * client2_name,
  182. dbus_uint64_t port2_id,
  183. const char * port2_name,
  184. dbus_uint64_t connection_id)
  185. {
  186. jack_dbus_send_signal(
  187. JACK_CONTROLLER_OBJECT_PATH,
  188. JACK_DBUS_IFACE_NAME,
  189. "PortsConnected",
  190. DBUS_TYPE_UINT64,
  191. &new_graph_version,
  192. DBUS_TYPE_UINT64,
  193. &client1_id,
  194. DBUS_TYPE_STRING,
  195. &client1_name,
  196. DBUS_TYPE_UINT64,
  197. &port1_id,
  198. DBUS_TYPE_STRING,
  199. &port1_name,
  200. DBUS_TYPE_UINT64,
  201. &client2_id,
  202. DBUS_TYPE_STRING,
  203. &client2_name,
  204. DBUS_TYPE_UINT64,
  205. &port2_id,
  206. DBUS_TYPE_STRING,
  207. &port2_name,
  208. DBUS_TYPE_UINT64,
  209. &connection_id,
  210. DBUS_TYPE_INVALID);
  211. }
  212. void
  213. jack_controller_patchbay_send_signal_ports_disconnected(
  214. dbus_uint64_t new_graph_version,
  215. dbus_uint64_t client1_id,
  216. const char * client1_name,
  217. dbus_uint64_t port1_id,
  218. const char * port1_name,
  219. dbus_uint64_t client2_id,
  220. const char * client2_name,
  221. dbus_uint64_t port2_id,
  222. const char * port2_name,
  223. dbus_uint64_t connection_id)
  224. {
  225. jack_dbus_send_signal(
  226. JACK_CONTROLLER_OBJECT_PATH,
  227. JACK_DBUS_IFACE_NAME,
  228. "PortsDisconnected",
  229. DBUS_TYPE_UINT64,
  230. &new_graph_version,
  231. DBUS_TYPE_UINT64,
  232. &client1_id,
  233. DBUS_TYPE_STRING,
  234. &client1_name,
  235. DBUS_TYPE_UINT64,
  236. &port1_id,
  237. DBUS_TYPE_STRING,
  238. &port1_name,
  239. DBUS_TYPE_UINT64,
  240. &client2_id,
  241. DBUS_TYPE_STRING,
  242. &client2_name,
  243. DBUS_TYPE_UINT64,
  244. &port2_id,
  245. DBUS_TYPE_STRING,
  246. &port2_name,
  247. DBUS_TYPE_UINT64,
  248. &connection_id,
  249. DBUS_TYPE_INVALID);
  250. }
  251. static
  252. struct jack_graph_client *
  253. jack_controller_patchbay_find_client(
  254. struct jack_controller_patchbay *patchbay_ptr,
  255. const char *client_name, /* not '\0' terminated */
  256. size_t client_name_len) /* without terminating '\0' */
  257. {
  258. struct list_head *node_ptr;
  259. struct jack_graph_client *client_ptr;
  260. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  261. {
  262. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  263. if (strncmp(client_ptr->name, client_name, client_name_len) == 0)
  264. {
  265. return client_ptr;
  266. }
  267. }
  268. return NULL;
  269. }
  270. static
  271. struct jack_graph_client *
  272. jack_controller_patchbay_find_client_by_id(
  273. struct jack_controller_patchbay *patchbay_ptr,
  274. uint64_t id)
  275. {
  276. struct list_head *node_ptr;
  277. struct jack_graph_client *client_ptr;
  278. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  279. {
  280. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  281. if (client_ptr->id == id)
  282. {
  283. return client_ptr;
  284. }
  285. }
  286. return NULL;
  287. }
  288. static
  289. struct jack_graph_client *
  290. jack_controller_patchbay_create_client(
  291. struct jack_controller_patchbay *patchbay_ptr,
  292. const char *client_name, /* not '\0' terminated */
  293. size_t client_name_len) /* without terminating '\0' */
  294. {
  295. struct jack_graph_client * client_ptr;
  296. client_ptr = malloc(sizeof(struct jack_graph_client));
  297. if (client_ptr == NULL)
  298. {
  299. jack_error("Memory allocation of jack_graph_client structure failed.");
  300. goto fail;
  301. }
  302. client_ptr->name = malloc(client_name_len + 1);
  303. if (client_ptr->name == NULL)
  304. {
  305. jack_error("malloc() failed to allocate memory for client name.");
  306. goto fail_free_client;
  307. }
  308. memcpy(client_ptr->name, client_name, client_name_len);
  309. client_ptr->name[client_name_len] = 0;
  310. client_ptr->pid = jack_get_client_pid(client_ptr->name);
  311. jack_info("New client '%s' with PID %d", client_ptr->name, client_ptr->pid);
  312. client_ptr->id = patchbay_ptr->next_client_id++;
  313. INIT_LIST_HEAD(&client_ptr->ports);
  314. pthread_mutex_lock(&patchbay_ptr->lock);
  315. list_add_tail(&client_ptr->siblings, &patchbay_ptr->graph.clients);
  316. patchbay_ptr->graph.version++;
  317. jack_controller_patchbay_send_signal_client_appeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  318. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  319. pthread_mutex_unlock(&patchbay_ptr->lock);
  320. return client_ptr;
  321. fail_free_client:
  322. free(client_ptr);
  323. fail:
  324. return NULL;
  325. }
  326. static
  327. void
  328. jack_controller_patchbay_destroy_client(
  329. struct jack_controller_patchbay *patchbay_ptr,
  330. struct jack_graph_client *client_ptr)
  331. {
  332. pthread_mutex_lock(&patchbay_ptr->lock);
  333. list_del(&client_ptr->siblings);
  334. patchbay_ptr->graph.version++;
  335. jack_controller_patchbay_send_signal_client_disappeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  336. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  337. pthread_mutex_unlock(&patchbay_ptr->lock);
  338. free(client_ptr->name);
  339. free(client_ptr);
  340. }
  341. static
  342. void
  343. jack_controller_patchbay_destroy_client_by_name(
  344. struct jack_controller_patchbay *patchbay_ptr,
  345. const char *client_name) /* '\0' terminated */
  346. {
  347. struct jack_graph_client *client_ptr;
  348. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  349. if (client_ptr == NULL)
  350. {
  351. jack_error("Cannot destroy unknown client '%s'", client_name);
  352. return;
  353. }
  354. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  355. }
  356. static
  357. void
  358. jack_controller_patchbay_new_port(
  359. struct jack_controller_patchbay *patchbay_ptr,
  360. const char *port_full_name,
  361. uint32_t port_flags,
  362. uint32_t port_type)
  363. {
  364. struct jack_graph_client *client_ptr;
  365. struct jack_graph_port *port_ptr;
  366. const char *port_short_name;
  367. size_t client_name_len;
  368. //jack_info("name: %s", port_full_name);
  369. port_short_name = strchr(port_full_name, ':');
  370. if (port_short_name == NULL)
  371. {
  372. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  373. return;
  374. }
  375. port_short_name++; /* skip ':' separator char */
  376. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  377. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  378. if (client_ptr == NULL)
  379. {
  380. client_ptr = jack_controller_patchbay_create_client(patchbay_ptr, port_full_name, client_name_len);
  381. if (client_ptr == NULL)
  382. {
  383. jack_error("Creation of new jack_graph client failed.");
  384. return;
  385. }
  386. }
  387. port_ptr = malloc(sizeof(struct jack_graph_port));
  388. if (port_ptr == NULL)
  389. {
  390. jack_error("Memory allocation of jack_graph_port structure failed.");
  391. return;
  392. }
  393. port_ptr->name = strdup(port_short_name);
  394. if (port_ptr->name == NULL)
  395. {
  396. jack_error("strdup() call for port name '%s' failed.", port_short_name);
  397. free(port_ptr);
  398. return;
  399. }
  400. port_ptr->id = patchbay_ptr->next_port_id++;
  401. port_ptr->flags = port_flags;
  402. port_ptr->type = port_type;
  403. port_ptr->client = client_ptr;
  404. pthread_mutex_lock(&patchbay_ptr->lock);
  405. list_add_tail(&port_ptr->siblings_client, &client_ptr->ports);
  406. list_add_tail(&port_ptr->siblings_graph, &patchbay_ptr->graph.ports);
  407. patchbay_ptr->graph.version++;
  408. jack_controller_patchbay_send_signal_port_appeared(
  409. patchbay_ptr->graph.version,
  410. client_ptr->id,
  411. client_ptr->name,
  412. port_ptr->id,
  413. port_ptr->name,
  414. port_ptr->flags,
  415. port_ptr->type);
  416. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  417. pthread_mutex_unlock(&patchbay_ptr->lock);
  418. }
  419. static
  420. void
  421. jack_controller_patchbay_remove_port(
  422. struct jack_controller_patchbay *patchbay_ptr,
  423. struct jack_graph_port *port_ptr)
  424. {
  425. pthread_mutex_lock(&patchbay_ptr->lock);
  426. list_del(&port_ptr->siblings_client);
  427. list_del(&port_ptr->siblings_graph);
  428. patchbay_ptr->graph.version++;
  429. 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);
  430. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  431. pthread_mutex_unlock(&patchbay_ptr->lock);
  432. free(port_ptr->name);
  433. free(port_ptr);
  434. }
  435. static
  436. struct jack_graph_port *
  437. jack_controller_patchbay_find_port_by_id(
  438. struct jack_controller_patchbay *patchbay_ptr,
  439. uint64_t port_id)
  440. {
  441. struct list_head *node_ptr;
  442. struct jack_graph_port *port_ptr;
  443. list_for_each(node_ptr, &patchbay_ptr->graph.ports)
  444. {
  445. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_graph);
  446. if (port_ptr->id == port_id)
  447. {
  448. return port_ptr;
  449. }
  450. }
  451. return NULL;
  452. }
  453. static
  454. struct jack_graph_port *
  455. jack_controller_patchbay_find_client_port_by_name(
  456. struct jack_controller_patchbay *patchbay_ptr,
  457. struct jack_graph_client *client_ptr,
  458. const char *port_name)
  459. {
  460. struct list_head *node_ptr;
  461. struct jack_graph_port *port_ptr;
  462. list_for_each(node_ptr, &client_ptr->ports)
  463. {
  464. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_client);
  465. if (strcmp(port_ptr->name, port_name) == 0)
  466. {
  467. return port_ptr;
  468. }
  469. }
  470. return NULL;
  471. }
  472. static
  473. struct jack_graph_port *
  474. jack_controller_patchbay_find_port_by_full_name(
  475. struct jack_controller_patchbay *patchbay_ptr,
  476. const char *port_full_name)
  477. {
  478. const char *port_short_name;
  479. size_t client_name_len;
  480. struct jack_graph_client *client_ptr;
  481. //jack_info("name: %s", port_full_name);
  482. port_short_name = strchr(port_full_name, ':');
  483. if (port_short_name == NULL)
  484. {
  485. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  486. return NULL;
  487. }
  488. port_short_name++; /* skip ':' separator char */
  489. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  490. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  491. if (client_ptr == NULL)
  492. {
  493. jack_error("cannot find client of port '%s'", port_full_name);
  494. return NULL;
  495. }
  496. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_short_name);
  497. }
  498. static
  499. struct jack_graph_port *
  500. jack_controller_patchbay_find_port_by_names(
  501. struct jack_controller_patchbay *patchbay_ptr,
  502. const char *client_name,
  503. const char *port_name)
  504. {
  505. struct jack_graph_client *client_ptr;
  506. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  507. if (client_ptr == NULL)
  508. {
  509. jack_error("cannot find client '%s'", client_name);
  510. return NULL;
  511. }
  512. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_name);
  513. }
  514. static
  515. struct jack_graph_connection *
  516. jack_controller_patchbay_create_connection(
  517. struct jack_controller_patchbay *patchbay_ptr,
  518. struct jack_graph_port *port1_ptr,
  519. struct jack_graph_port *port2_ptr)
  520. {
  521. struct jack_graph_connection * connection_ptr;
  522. connection_ptr = malloc(sizeof(struct jack_graph_connection));
  523. if (connection_ptr == NULL)
  524. {
  525. jack_error("Memory allocation of jack_graph_connection structure failed.");
  526. return NULL;
  527. }
  528. connection_ptr->id = patchbay_ptr->next_connection_id++;
  529. connection_ptr->port1 = port1_ptr;
  530. connection_ptr->port2 = port2_ptr;
  531. pthread_mutex_lock(&patchbay_ptr->lock);
  532. list_add_tail(&connection_ptr->siblings, &patchbay_ptr->graph.connections);
  533. patchbay_ptr->graph.version++;
  534. jack_controller_patchbay_send_signal_ports_connected(
  535. patchbay_ptr->graph.version,
  536. port1_ptr->client->id,
  537. port1_ptr->client->name,
  538. port1_ptr->id,
  539. port1_ptr->name,
  540. port2_ptr->client->id,
  541. port2_ptr->client->name,
  542. port2_ptr->id,
  543. port2_ptr->name,
  544. connection_ptr->id);
  545. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  546. pthread_mutex_unlock(&patchbay_ptr->lock);
  547. return connection_ptr;
  548. }
  549. static
  550. void
  551. jack_controller_patchbay_destroy_connection(
  552. struct jack_controller_patchbay *patchbay_ptr,
  553. struct jack_graph_connection *connection_ptr)
  554. {
  555. pthread_mutex_lock(&patchbay_ptr->lock);
  556. list_del(&connection_ptr->siblings);
  557. patchbay_ptr->graph.version++;
  558. jack_controller_patchbay_send_signal_ports_disconnected(
  559. patchbay_ptr->graph.version,
  560. connection_ptr->port1->client->id,
  561. connection_ptr->port1->client->name,
  562. connection_ptr->port1->id,
  563. connection_ptr->port1->name,
  564. connection_ptr->port2->client->id,
  565. connection_ptr->port2->client->name,
  566. connection_ptr->port2->id,
  567. connection_ptr->port2->name,
  568. connection_ptr->id);
  569. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  570. pthread_mutex_unlock(&patchbay_ptr->lock);
  571. free(connection_ptr);
  572. }
  573. static
  574. struct jack_graph_connection *
  575. jack_controller_patchbay_find_connection(
  576. struct jack_controller_patchbay *patchbay_ptr,
  577. struct jack_graph_port *port1_ptr,
  578. struct jack_graph_port *port2_ptr)
  579. {
  580. struct list_head *node_ptr;
  581. struct jack_graph_connection *connection_ptr;
  582. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  583. {
  584. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  585. if ((connection_ptr->port1 == port1_ptr &&
  586. connection_ptr->port2 == port2_ptr) ||
  587. (connection_ptr->port1 == port2_ptr &&
  588. connection_ptr->port2 == port1_ptr))
  589. {
  590. return connection_ptr;
  591. }
  592. }
  593. return NULL;
  594. }
  595. static
  596. struct jack_graph_connection *
  597. jack_controller_patchbay_find_connection_by_id(
  598. struct jack_controller_patchbay *patchbay_ptr,
  599. uint64_t connection_id)
  600. {
  601. struct list_head *node_ptr;
  602. struct jack_graph_connection *connection_ptr;
  603. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  604. {
  605. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  606. if (connection_ptr->id == connection_id)
  607. {
  608. return connection_ptr;
  609. }
  610. }
  611. return NULL;
  612. }
  613. static
  614. bool
  615. jack_controller_patchbay_connect(
  616. struct jack_dbus_method_call *dbus_call_ptr,
  617. struct jack_controller *controller_ptr,
  618. struct jack_graph_port *port1_ptr,
  619. struct jack_graph_port *port2_ptr)
  620. {
  621. int ret;
  622. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  623. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  624. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  625. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  626. ret = jack_connect(controller_ptr->client, port1_name, port2_name);
  627. if (ret != 0)
  628. {
  629. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  630. return false;
  631. }
  632. return true;
  633. }
  634. static
  635. bool
  636. jack_controller_patchbay_disconnect(
  637. struct jack_dbus_method_call *dbus_call_ptr,
  638. struct jack_controller *controller_ptr,
  639. struct jack_graph_port *port1_ptr,
  640. struct jack_graph_port *port2_ptr)
  641. {
  642. int ret;
  643. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  644. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  645. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  646. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  647. ret = jack_disconnect(controller_ptr->client, port1_name, port2_name);
  648. if (ret != 0)
  649. {
  650. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  651. return false;
  652. }
  653. return true;
  654. }
  655. #define controller_ptr ((struct jack_controller *)call->context)
  656. #define patchbay_ptr ((struct jack_controller_patchbay *)controller_ptr->patchbay_context)
  657. static
  658. void
  659. jack_controller_dbus_get_all_ports(
  660. struct jack_dbus_method_call * call)
  661. {
  662. struct list_head * client_node_ptr;
  663. struct list_head * port_node_ptr;
  664. struct jack_graph_client * client_ptr;
  665. struct jack_graph_port * port_ptr;
  666. DBusMessageIter iter, sub_iter;
  667. char fullname[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  668. char *fullname_var = fullname;
  669. if (!controller_ptr->started)
  670. {
  671. jack_dbus_error(
  672. call,
  673. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  674. "Can't execute this method with stopped JACK server");
  675. return;
  676. }
  677. call->reply = dbus_message_new_method_return (call->message);
  678. if (!call->reply)
  679. {
  680. goto fail;
  681. }
  682. dbus_message_iter_init_append (call->reply, &iter);
  683. if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
  684. {
  685. goto fail_unref;
  686. }
  687. pthread_mutex_lock(&patchbay_ptr->lock);
  688. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  689. {
  690. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  691. list_for_each(port_node_ptr, &client_ptr->ports)
  692. {
  693. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  694. jack_info("%s:%s", client_ptr->name, port_ptr->name);
  695. sprintf(fullname, "%s:%s", client_ptr->name, port_ptr->name);
  696. if (!dbus_message_iter_append_basic (&sub_iter, DBUS_TYPE_STRING, &fullname_var))
  697. {
  698. pthread_mutex_unlock(&patchbay_ptr->lock);
  699. dbus_message_iter_close_container (&iter, &sub_iter);
  700. goto fail_unref;
  701. }
  702. }
  703. }
  704. pthread_mutex_unlock(&patchbay_ptr->lock);
  705. if (!dbus_message_iter_close_container (&iter, &sub_iter))
  706. {
  707. goto fail_unref;
  708. }
  709. return;
  710. fail_unref:
  711. dbus_message_unref (call->reply);
  712. call->reply = NULL;
  713. fail:
  714. jack_error ("Ran out of memory trying to construct method return");
  715. }
  716. static
  717. void
  718. jack_controller_dbus_get_graph(
  719. struct jack_dbus_method_call * call)
  720. {
  721. struct list_head * client_node_ptr;
  722. struct list_head * port_node_ptr;
  723. struct list_head * connection_node_ptr;
  724. struct jack_graph_client * client_ptr;
  725. struct jack_graph_port * port_ptr;
  726. struct jack_graph_connection * connection_ptr;
  727. DBusMessageIter iter;
  728. DBusMessageIter clients_array_iter;
  729. DBusMessageIter client_struct_iter;
  730. DBusMessageIter ports_array_iter;
  731. DBusMessageIter port_struct_iter;
  732. dbus_uint64_t version;
  733. DBusMessageIter connections_array_iter;
  734. DBusMessageIter connection_struct_iter;
  735. if (!controller_ptr->started)
  736. {
  737. jack_dbus_error(
  738. call,
  739. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  740. "Can't execute this method with stopped JACK server");
  741. return;
  742. }
  743. if (!jack_dbus_get_method_args(call, DBUS_TYPE_UINT64, &version, DBUS_TYPE_INVALID))
  744. {
  745. /* The method call had invalid arguments meaning that
  746. * jack_dbus_get_method_args() has constructed an error for us.
  747. */
  748. goto exit;
  749. }
  750. //jack_info("Getting graph, know version is %" PRIu32, version);
  751. call->reply = dbus_message_new_method_return(call->message);
  752. if (!call->reply)
  753. {
  754. jack_error("Ran out of memory trying to construct method return");
  755. goto exit;
  756. }
  757. dbus_message_iter_init_append (call->reply, &iter);
  758. pthread_mutex_lock(&patchbay_ptr->lock);
  759. if (version > patchbay_ptr->graph.version)
  760. {
  761. jack_dbus_error(
  762. call,
  763. JACK_DBUS_ERROR_INVALID_ARGS,
  764. "known graph version %" PRIu64 " is newer than actual version %" PRIu64,
  765. version,
  766. patchbay_ptr->graph.version);
  767. pthread_mutex_unlock(&patchbay_ptr->lock);
  768. goto exit;
  769. }
  770. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &patchbay_ptr->graph.version))
  771. {
  772. goto nomem_unlock;
  773. }
  774. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tsa(tsuu))", &clients_array_iter))
  775. {
  776. goto nomem_unlock;
  777. }
  778. if (version < patchbay_ptr->graph.version)
  779. {
  780. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  781. {
  782. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  783. if (!dbus_message_iter_open_container (&clients_array_iter, DBUS_TYPE_STRUCT, NULL, &client_struct_iter))
  784. {
  785. goto nomem_close_clients_array;
  786. }
  787. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_UINT64, &client_ptr->id))
  788. {
  789. goto nomem_close_client_struct;
  790. }
  791. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_STRING, &client_ptr->name))
  792. {
  793. goto nomem_close_client_struct;
  794. }
  795. if (!dbus_message_iter_open_container(&client_struct_iter, DBUS_TYPE_ARRAY, "(tsuu)", &ports_array_iter))
  796. {
  797. goto nomem_close_client_struct;
  798. }
  799. list_for_each(port_node_ptr, &client_ptr->ports)
  800. {
  801. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  802. if (!dbus_message_iter_open_container(&ports_array_iter, DBUS_TYPE_STRUCT, NULL, &port_struct_iter))
  803. {
  804. goto nomem_close_ports_array;
  805. }
  806. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT64, &port_ptr->id))
  807. {
  808. goto nomem_close_port_struct;
  809. }
  810. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_STRING, &port_ptr->name))
  811. {
  812. goto nomem_close_port_struct;
  813. }
  814. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->flags))
  815. {
  816. goto nomem_close_port_struct;
  817. }
  818. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->type))
  819. {
  820. goto nomem_close_port_struct;
  821. }
  822. if (!dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter))
  823. {
  824. goto nomem_close_ports_array;
  825. }
  826. }
  827. if (!dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter))
  828. {
  829. goto nomem_close_client_struct;
  830. }
  831. if (!dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter))
  832. {
  833. goto nomem_close_clients_array;
  834. }
  835. }
  836. }
  837. if (!dbus_message_iter_close_container(&iter, &clients_array_iter))
  838. {
  839. goto nomem_unlock;
  840. }
  841. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tstststst)", &connections_array_iter))
  842. {
  843. goto nomem_unlock;
  844. }
  845. if (version < patchbay_ptr->graph.version)
  846. {
  847. list_for_each(connection_node_ptr, &patchbay_ptr->graph.connections)
  848. {
  849. connection_ptr = list_entry(connection_node_ptr, struct jack_graph_connection, siblings);
  850. if (!dbus_message_iter_open_container(&connections_array_iter, DBUS_TYPE_STRUCT, NULL, &connection_struct_iter))
  851. {
  852. goto nomem_close_connections_array;
  853. }
  854. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->client->id))
  855. {
  856. goto nomem_close_connection_struct;
  857. }
  858. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->client->name))
  859. {
  860. goto nomem_close_connection_struct;
  861. }
  862. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->id))
  863. {
  864. goto nomem_close_connection_struct;
  865. }
  866. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->name))
  867. {
  868. goto nomem_close_connection_struct;
  869. }
  870. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->client->id))
  871. {
  872. goto nomem_close_connection_struct;
  873. }
  874. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->client->name))
  875. {
  876. goto nomem_close_connection_struct;
  877. }
  878. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->id))
  879. {
  880. goto nomem_close_connection_struct;
  881. }
  882. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->name))
  883. {
  884. goto nomem_close_connection_struct;
  885. }
  886. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->id))
  887. {
  888. goto nomem_close_connection_struct;
  889. }
  890. if (!dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter))
  891. {
  892. goto nomem_close_connections_array;
  893. }
  894. }
  895. }
  896. if (!dbus_message_iter_close_container(&iter, &connections_array_iter))
  897. {
  898. goto nomem_unlock;
  899. }
  900. pthread_mutex_unlock(&patchbay_ptr->lock);
  901. return;
  902. nomem_close_connection_struct:
  903. dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter);
  904. nomem_close_connections_array:
  905. dbus_message_iter_close_container(&iter, &connections_array_iter);
  906. goto nomem_unlock;
  907. nomem_close_port_struct:
  908. dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter);
  909. nomem_close_ports_array:
  910. dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter);
  911. nomem_close_client_struct:
  912. dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter);
  913. nomem_close_clients_array:
  914. dbus_message_iter_close_container(&iter, &clients_array_iter);
  915. nomem_unlock:
  916. pthread_mutex_unlock(&patchbay_ptr->lock);
  917. //nomem:
  918. dbus_message_unref(call->reply);
  919. call->reply = NULL;
  920. jack_error("Ran out of memory trying to construct method return");
  921. exit:
  922. return;
  923. }
  924. static
  925. void
  926. jack_controller_dbus_connect_ports_by_name(
  927. struct jack_dbus_method_call * call)
  928. {
  929. const char * client1_name;
  930. const char * port1_name;
  931. const char * client2_name;
  932. const char * port2_name;
  933. struct jack_graph_port *port1_ptr;
  934. struct jack_graph_port *port2_ptr;
  935. /* jack_info("jack_controller_dbus_connect_ports_by_name() called."); */
  936. if (!controller_ptr->started)
  937. {
  938. jack_dbus_error(
  939. call,
  940. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  941. "Can't execute this method with stopped JACK server");
  942. return;
  943. }
  944. if (!jack_dbus_get_method_args(
  945. call,
  946. DBUS_TYPE_STRING,
  947. &client1_name,
  948. DBUS_TYPE_STRING,
  949. &port1_name,
  950. DBUS_TYPE_STRING,
  951. &client2_name,
  952. DBUS_TYPE_STRING,
  953. &port2_name,
  954. DBUS_TYPE_INVALID))
  955. {
  956. /* The method call had invalid arguments meaning that
  957. * jack_dbus_get_method_args() has constructed an error for us.
  958. */
  959. return;
  960. }
  961. /* jack_info("connecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  962. pthread_mutex_lock(&patchbay_ptr->lock);
  963. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  964. if (port1_ptr == NULL)
  965. {
  966. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  967. goto unlock;
  968. }
  969. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  970. if (port2_ptr == NULL)
  971. {
  972. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  973. goto unlock;
  974. }
  975. if (!jack_controller_patchbay_connect(
  976. call,
  977. controller_ptr,
  978. port1_ptr,
  979. port2_ptr))
  980. {
  981. /* jack_controller_patchbay_connect() constructed error reply */
  982. goto unlock;
  983. }
  984. jack_dbus_construct_method_return_empty(call);
  985. unlock:
  986. pthread_mutex_unlock(&patchbay_ptr->lock);
  987. }
  988. static
  989. void
  990. jack_controller_dbus_connect_ports_by_id(
  991. struct jack_dbus_method_call * call)
  992. {
  993. dbus_uint64_t port1_id;
  994. dbus_uint64_t port2_id;
  995. struct jack_graph_port *port1_ptr;
  996. struct jack_graph_port *port2_ptr;
  997. /* jack_info("jack_controller_dbus_connect_ports_by_id() called."); */
  998. if (!controller_ptr->started)
  999. {
  1000. jack_dbus_error(
  1001. call,
  1002. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  1003. "Can't execute this method with stopped JACK server");
  1004. return;
  1005. }
  1006. if (!jack_dbus_get_method_args(
  1007. call,
  1008. DBUS_TYPE_UINT64,
  1009. &port1_id,
  1010. DBUS_TYPE_UINT64,
  1011. &port2_id,
  1012. DBUS_TYPE_INVALID))
  1013. {
  1014. /* The method call had invalid arguments meaning that
  1015. * jack_dbus_get_method_args() has constructed an error for us.
  1016. */
  1017. return;
  1018. }
  1019. pthread_mutex_lock(&patchbay_ptr->lock);
  1020. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  1021. if (port1_ptr == NULL)
  1022. {
  1023. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  1024. goto unlock;
  1025. }
  1026. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  1027. if (port2_ptr == NULL)
  1028. {
  1029. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  1030. goto unlock;
  1031. }
  1032. if (!jack_controller_patchbay_connect(
  1033. call,
  1034. controller_ptr,
  1035. port1_ptr,
  1036. port2_ptr))
  1037. {
  1038. /* jack_controller_patchbay_connect() constructed error reply */
  1039. goto unlock;
  1040. }
  1041. jack_dbus_construct_method_return_empty(call);
  1042. unlock:
  1043. pthread_mutex_unlock(&patchbay_ptr->lock);
  1044. }
  1045. static
  1046. void
  1047. jack_controller_dbus_disconnect_ports_by_name(
  1048. struct jack_dbus_method_call * call)
  1049. {
  1050. const char * client1_name;
  1051. const char * port1_name;
  1052. const char * client2_name;
  1053. const char * port2_name;
  1054. struct jack_graph_port *port1_ptr;
  1055. struct jack_graph_port *port2_ptr;
  1056. /* jack_info("jack_controller_dbus_disconnect_ports_by_name() called."); */
  1057. if (!controller_ptr->started)
  1058. {
  1059. jack_dbus_error(
  1060. call,
  1061. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  1062. "Can't execute this method with stopped JACK server");
  1063. return;
  1064. }
  1065. if (!jack_dbus_get_method_args(
  1066. call,
  1067. DBUS_TYPE_STRING,
  1068. &client1_name,
  1069. DBUS_TYPE_STRING,
  1070. &port1_name,
  1071. DBUS_TYPE_STRING,
  1072. &client2_name,
  1073. DBUS_TYPE_STRING,
  1074. &port2_name,
  1075. DBUS_TYPE_INVALID))
  1076. {
  1077. /* The method call had invalid arguments meaning that
  1078. * jack_dbus_get_method_args() has constructed an error for us.
  1079. */
  1080. return;
  1081. }
  1082. /* jack_info("disconnecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  1083. pthread_mutex_lock(&patchbay_ptr->lock);
  1084. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  1085. if (port1_ptr == NULL)
  1086. {
  1087. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  1088. goto unlock;
  1089. }
  1090. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  1091. if (port2_ptr == NULL)
  1092. {
  1093. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  1094. goto unlock;
  1095. }
  1096. if (!jack_controller_patchbay_disconnect(
  1097. call,
  1098. controller_ptr,
  1099. port1_ptr,
  1100. port2_ptr))
  1101. {
  1102. /* jack_controller_patchbay_connect() constructed error reply */
  1103. goto unlock;
  1104. }
  1105. jack_dbus_construct_method_return_empty(call);
  1106. unlock:
  1107. pthread_mutex_unlock(&patchbay_ptr->lock);
  1108. }
  1109. static
  1110. void
  1111. jack_controller_dbus_disconnect_ports_by_id(
  1112. struct jack_dbus_method_call * call)
  1113. {
  1114. dbus_uint64_t port1_id;
  1115. dbus_uint64_t port2_id;
  1116. struct jack_graph_port *port1_ptr;
  1117. struct jack_graph_port *port2_ptr;
  1118. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  1119. if (!controller_ptr->started)
  1120. {
  1121. jack_dbus_error(
  1122. call,
  1123. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  1124. "Can't execute this method with stopped JACK server");
  1125. return;
  1126. }
  1127. if (!jack_dbus_get_method_args(
  1128. call,
  1129. DBUS_TYPE_UINT64,
  1130. &port1_id,
  1131. DBUS_TYPE_UINT64,
  1132. &port2_id,
  1133. DBUS_TYPE_INVALID))
  1134. {
  1135. /* The method call had invalid arguments meaning that
  1136. * jack_dbus_get_method_args() has constructed an error for us.
  1137. */
  1138. return;
  1139. }
  1140. pthread_mutex_lock(&patchbay_ptr->lock);
  1141. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  1142. if (port1_ptr == NULL)
  1143. {
  1144. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  1145. return;
  1146. }
  1147. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  1148. if (port2_ptr == NULL)
  1149. {
  1150. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  1151. return;
  1152. }
  1153. if (!jack_controller_patchbay_disconnect(
  1154. call,
  1155. controller_ptr,
  1156. port1_ptr,
  1157. port2_ptr))
  1158. {
  1159. /* jack_controller_patchbay_connect() constructed error reply */
  1160. goto unlock;
  1161. }
  1162. jack_dbus_construct_method_return_empty(call);
  1163. unlock:
  1164. pthread_mutex_unlock(&patchbay_ptr->lock);
  1165. }
  1166. static
  1167. void
  1168. jack_controller_dbus_disconnect_ports_by_connection_id(
  1169. struct jack_dbus_method_call * call)
  1170. {
  1171. dbus_uint64_t connection_id;
  1172. struct jack_graph_connection *connection_ptr;
  1173. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  1174. if (!jack_dbus_get_method_args(
  1175. call,
  1176. DBUS_TYPE_UINT64,
  1177. &connection_id,
  1178. DBUS_TYPE_INVALID))
  1179. {
  1180. /* The method call had invalid arguments meaning that
  1181. * jack_dbus_get_method_args() has constructed an error for us.
  1182. */
  1183. return;
  1184. }
  1185. pthread_mutex_lock(&patchbay_ptr->lock);
  1186. connection_ptr = jack_controller_patchbay_find_connection_by_id(patchbay_ptr, connection_id);
  1187. if (connection_ptr == NULL)
  1188. {
  1189. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find connection %" PRIu64, connection_id);
  1190. goto unlock;
  1191. }
  1192. if (!jack_controller_patchbay_disconnect(
  1193. call,
  1194. controller_ptr,
  1195. connection_ptr->port1,
  1196. connection_ptr->port2))
  1197. {
  1198. /* jack_controller_patchbay_connect() constructed error reply */
  1199. goto unlock;
  1200. }
  1201. jack_dbus_construct_method_return_empty(call);
  1202. unlock:
  1203. pthread_mutex_unlock(&patchbay_ptr->lock);
  1204. }
  1205. static
  1206. void
  1207. jack_controller_dbus_get_client_pid(
  1208. struct jack_dbus_method_call * call)
  1209. {
  1210. dbus_uint64_t client_id;
  1211. struct jack_graph_client *client_ptr;
  1212. message_arg_t arg;
  1213. /* jack_info("jack_controller_dbus_get_client_pid() called."); */
  1214. if (!jack_dbus_get_method_args(
  1215. call,
  1216. DBUS_TYPE_UINT64,
  1217. &client_id,
  1218. DBUS_TYPE_INVALID))
  1219. {
  1220. /* The method call had invalid arguments meaning that
  1221. * jack_dbus_get_method_args() has constructed an error for us.
  1222. */
  1223. return;
  1224. }
  1225. pthread_mutex_lock(&patchbay_ptr->lock);
  1226. client_ptr = jack_controller_patchbay_find_client_by_id(patchbay_ptr, client_id);
  1227. if (client_ptr == NULL)
  1228. {
  1229. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find client %" PRIu64, client_id);
  1230. goto unlock;
  1231. }
  1232. arg.int64 = client_ptr->pid;
  1233. jack_dbus_construct_method_return_single(call, DBUS_TYPE_INT64, arg);
  1234. unlock:
  1235. pthread_mutex_unlock(&patchbay_ptr->lock);
  1236. }
  1237. #undef controller_ptr
  1238. #define controller_ptr ((struct jack_controller *)context)
  1239. static
  1240. int
  1241. jack_controller_graph_order_callback(
  1242. void *context)
  1243. {
  1244. const char **ports;
  1245. int i;
  1246. jack_port_t *port_ptr;
  1247. if (patchbay_ptr->graph.version > 1)
  1248. {
  1249. /* we use this only for initial catchup */
  1250. return 0;
  1251. }
  1252. ports = jack_get_ports(controller_ptr->client, NULL, NULL, 0);
  1253. if (ports)
  1254. {
  1255. for (i = 0; ports[i]; ++i)
  1256. {
  1257. jack_info("graph reorder: new port '%s'", ports[i]);
  1258. port_ptr = jack_port_by_name(controller_ptr->client, ports[i]);;
  1259. jack_controller_patchbay_new_port(patchbay_ptr, ports[i], jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  1260. }
  1261. free(ports);
  1262. }
  1263. if (patchbay_ptr->graph.version == 1)
  1264. {
  1265. /* we have empty initial graph, increment graph version,
  1266. so we dont do jack_get_ports() again,
  1267. on next next graph change */
  1268. patchbay_ptr->graph.version++;
  1269. }
  1270. return 0;
  1271. }
  1272. void
  1273. jack_controller_client_registration_callback(
  1274. const char *client_name,
  1275. int created,
  1276. void *context)
  1277. {
  1278. if (created)
  1279. {
  1280. jack_log("client '%s' created", client_name);
  1281. jack_controller_patchbay_create_client(patchbay_ptr, client_name, strlen(client_name));
  1282. }
  1283. else
  1284. {
  1285. jack_log("client '%s' destroyed", client_name);
  1286. jack_controller_patchbay_destroy_client_by_name(patchbay_ptr, client_name);
  1287. }
  1288. }
  1289. void
  1290. jack_controller_port_registration_callback(
  1291. jack_port_id_t port_id,
  1292. int created,
  1293. void *context)
  1294. {
  1295. jack_port_t *port_ptr;
  1296. struct jack_graph_port *graph_port_ptr;
  1297. const char *port_name;
  1298. port_ptr = jack_port_by_id(controller_ptr->client, port_id);
  1299. port_name = jack_port_name(port_ptr);
  1300. if (created)
  1301. {
  1302. jack_log("port '%s' created", port_name);
  1303. jack_controller_patchbay_new_port(patchbay_ptr, port_name, jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  1304. }
  1305. else
  1306. {
  1307. jack_log("port '%s' destroyed", port_name);
  1308. graph_port_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port_name);
  1309. if (graph_port_ptr == NULL)
  1310. {
  1311. jack_error("Failed to find port '%s' to destroy", port_name);
  1312. return;
  1313. }
  1314. jack_controller_patchbay_remove_port(patchbay_ptr, graph_port_ptr);
  1315. }
  1316. }
  1317. void
  1318. jack_controller_port_connect_callback(
  1319. jack_port_id_t port1_id,
  1320. jack_port_id_t port2_id,
  1321. int connect,
  1322. void *context)
  1323. {
  1324. jack_port_t *port1;
  1325. jack_port_t *port2;
  1326. const char *port1_name;
  1327. const char *port2_name;
  1328. struct jack_graph_port *port1_ptr;
  1329. struct jack_graph_port *port2_ptr;
  1330. struct jack_graph_connection *connection_ptr;
  1331. port1 = jack_port_by_id(controller_ptr->client, port1_id);
  1332. port2 = jack_port_by_id(controller_ptr->client, port2_id);
  1333. port1_name = jack_port_name(port1);
  1334. port2_name = jack_port_name(port2);
  1335. port1_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port1_name);
  1336. if (port1_ptr == NULL)
  1337. {
  1338. jack_error("Failed to find port '%s' to [dis]connect", port1_name);
  1339. return;
  1340. }
  1341. port2_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port2_name);
  1342. if (port2_ptr == NULL)
  1343. {
  1344. jack_error("Failed to find port '%s' to [dis]connect", port2_name);
  1345. return;
  1346. }
  1347. if (connect)
  1348. {
  1349. jack_info("Connecting '%s' to '%s'", port1_name, port2_name);
  1350. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  1351. if (connection_ptr != NULL)
  1352. {
  1353. jack_error("'%s' and '%s' are already connected", port1_name, port2_name);
  1354. return;
  1355. }
  1356. jack_controller_patchbay_create_connection(patchbay_ptr, port1_ptr, port2_ptr);
  1357. }
  1358. else
  1359. {
  1360. jack_info("Disonnecting '%s' from '%s'", port1_name, port2_name);
  1361. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  1362. if (connection_ptr == NULL)
  1363. {
  1364. jack_error("Cannot find connection being removed");
  1365. return;
  1366. }
  1367. jack_controller_patchbay_destroy_connection(patchbay_ptr, connection_ptr);
  1368. }
  1369. }
  1370. #undef controller_ptr
  1371. void
  1372. jack_controller_patchbay_uninit(
  1373. struct jack_controller * controller_ptr)
  1374. {
  1375. struct jack_graph_client *client_ptr;
  1376. struct jack_graph_port *port_ptr;
  1377. /* jack_info("jack_controller_patchbay_uninit() called"); */
  1378. while (!list_empty(&patchbay_ptr->graph.ports))
  1379. {
  1380. port_ptr = list_entry(patchbay_ptr->graph.ports.next, struct jack_graph_port, siblings_graph);
  1381. jack_controller_patchbay_remove_port(patchbay_ptr, port_ptr);
  1382. }
  1383. while (!list_empty(&patchbay_ptr->graph.clients))
  1384. {
  1385. client_ptr = list_entry(patchbay_ptr->graph.clients.next, struct jack_graph_client, siblings);
  1386. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  1387. }
  1388. pthread_mutex_destroy(&patchbay_ptr->lock);
  1389. }
  1390. #undef patchbay_ptr
  1391. bool
  1392. jack_controller_patchbay_init(
  1393. struct jack_controller * controller_ptr)
  1394. {
  1395. int ret;
  1396. struct jack_controller_patchbay * patchbay_ptr;
  1397. pthread_mutexattr_t attr;
  1398. /* jack_info("jack_controller_patchbay_init() called"); */
  1399. patchbay_ptr = malloc(sizeof(struct jack_controller_patchbay));
  1400. if (patchbay_ptr == NULL)
  1401. {
  1402. jack_error("Memory allocation of jack_controller_patchbay structure failed.");
  1403. goto fail;
  1404. }
  1405. ret = pthread_mutexattr_init(&attr);
  1406. if (ret != 0)
  1407. {
  1408. goto fail;
  1409. }
  1410. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  1411. if (ret != 0)
  1412. {
  1413. goto fail;
  1414. }
  1415. pthread_mutex_init(&patchbay_ptr->lock, &attr);
  1416. INIT_LIST_HEAD(&patchbay_ptr->graph.clients);
  1417. INIT_LIST_HEAD(&patchbay_ptr->graph.ports);
  1418. INIT_LIST_HEAD(&patchbay_ptr->graph.connections);
  1419. patchbay_ptr->graph.version = 1;
  1420. patchbay_ptr->next_client_id = 1;
  1421. patchbay_ptr->next_port_id = 1;
  1422. patchbay_ptr->next_connection_id = 1;
  1423. controller_ptr->patchbay_context = patchbay_ptr;
  1424. ret = jack_set_graph_order_callback(controller_ptr->client, jack_controller_graph_order_callback, controller_ptr);
  1425. if (ret != 0)
  1426. {
  1427. jack_error("jack_set_graph_order_callback() failed with error %d", ret);
  1428. goto fail_uninit_mutex;
  1429. }
  1430. ret = jack_set_client_registration_callback(controller_ptr->client, jack_controller_client_registration_callback, controller_ptr);
  1431. if (ret != 0)
  1432. {
  1433. jack_error("jack_set_client_registration_callback() failed with error %d", ret);
  1434. goto fail_uninit_mutex;
  1435. }
  1436. ret = jack_set_port_registration_callback(controller_ptr->client, jack_controller_port_registration_callback, controller_ptr);
  1437. if (ret != 0)
  1438. {
  1439. jack_error("jack_set_port_registration_callback() failed with error %d", ret);
  1440. goto fail_uninit_mutex;
  1441. }
  1442. ret = jack_set_port_connect_callback(controller_ptr->client, jack_controller_port_connect_callback, controller_ptr);
  1443. if (ret != 0)
  1444. {
  1445. jack_error("jack_set_port_connect_callback() failed with error %d", ret);
  1446. goto fail_uninit_mutex;
  1447. }
  1448. return true;
  1449. fail_uninit_mutex:
  1450. pthread_mutex_destroy(&patchbay_ptr->lock);
  1451. fail:
  1452. return false;
  1453. }
  1454. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetAllPorts)
  1455. JACK_DBUS_METHOD_ARGUMENT("ports_list", "as", true)
  1456. JACK_DBUS_METHOD_ARGUMENTS_END
  1457. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetGraph)
  1458. JACK_DBUS_METHOD_ARGUMENT("known_graph_version", DBUS_TYPE_UINT64_AS_STRING, false)
  1459. JACK_DBUS_METHOD_ARGUMENT("current_graph_version", DBUS_TYPE_UINT64_AS_STRING, true)
  1460. JACK_DBUS_METHOD_ARGUMENT("clients_and_ports", "a(tsa(tsuu))", true)
  1461. JACK_DBUS_METHOD_ARGUMENT("connections", "a(tstststst)", true)
  1462. JACK_DBUS_METHOD_ARGUMENTS_END
  1463. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByName)
  1464. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  1465. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  1466. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  1467. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  1468. JACK_DBUS_METHOD_ARGUMENTS_END
  1469. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByID)
  1470. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  1471. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  1472. JACK_DBUS_METHOD_ARGUMENTS_END
  1473. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByName)
  1474. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  1475. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  1476. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  1477. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  1478. JACK_DBUS_METHOD_ARGUMENTS_END
  1479. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByID)
  1480. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  1481. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  1482. JACK_DBUS_METHOD_ARGUMENTS_END
  1483. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByConnectionID)
  1484. JACK_DBUS_METHOD_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING, false)
  1485. JACK_DBUS_METHOD_ARGUMENTS_END
  1486. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetClientPID)
  1487. JACK_DBUS_METHOD_ARGUMENT("client_id", DBUS_TYPE_INT64_AS_STRING, false)
  1488. JACK_DBUS_METHOD_ARGUMENTS_END
  1489. JACK_DBUS_METHODS_BEGIN
  1490. JACK_DBUS_METHOD_DESCRIBE(GetAllPorts, jack_controller_dbus_get_all_ports)
  1491. JACK_DBUS_METHOD_DESCRIBE(GetGraph, jack_controller_dbus_get_graph)
  1492. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByName, jack_controller_dbus_connect_ports_by_name)
  1493. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByID, jack_controller_dbus_connect_ports_by_id)
  1494. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByName, jack_controller_dbus_disconnect_ports_by_name)
  1495. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByID, jack_controller_dbus_disconnect_ports_by_id)
  1496. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByConnectionID, jack_controller_dbus_disconnect_ports_by_connection_id)
  1497. JACK_DBUS_METHOD_DESCRIBE(GetClientPID, jack_controller_dbus_get_client_pid)
  1498. JACK_DBUS_METHODS_END
  1499. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(GraphChanged)
  1500. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  1501. JACK_DBUS_SIGNAL_ARGUMENTS_END
  1502. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientAppeared)
  1503. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  1504. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  1505. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  1506. JACK_DBUS_SIGNAL_ARGUMENTS_END
  1507. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientDisappeared)
  1508. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  1509. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  1510. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  1511. JACK_DBUS_SIGNAL_ARGUMENTS_END
  1512. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortAppeared)
  1513. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  1514. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  1515. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  1516. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  1517. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  1518. JACK_DBUS_SIGNAL_ARGUMENT("port_flags", DBUS_TYPE_UINT32_AS_STRING)
  1519. JACK_DBUS_SIGNAL_ARGUMENT("port_type", DBUS_TYPE_UINT32_AS_STRING)
  1520. JACK_DBUS_SIGNAL_ARGUMENTS_END
  1521. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortDisappeared)
  1522. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  1523. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  1524. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  1525. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  1526. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  1527. JACK_DBUS_SIGNAL_ARGUMENTS_END
  1528. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsConnected)
  1529. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  1530. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  1531. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  1532. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  1533. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  1534. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  1535. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  1536. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  1537. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  1538. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  1539. JACK_DBUS_SIGNAL_ARGUMENTS_END
  1540. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsDisconnected)
  1541. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  1542. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  1543. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  1544. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  1545. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  1546. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  1547. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  1548. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  1549. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  1550. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  1551. JACK_DBUS_SIGNAL_ARGUMENTS_END
  1552. JACK_DBUS_SIGNALS_BEGIN
  1553. JACK_DBUS_SIGNAL_DESCRIBE(GraphChanged)
  1554. JACK_DBUS_SIGNAL_DESCRIBE(ClientAppeared)
  1555. JACK_DBUS_SIGNAL_DESCRIBE(ClientDisappeared)
  1556. JACK_DBUS_SIGNAL_DESCRIBE(PortAppeared)
  1557. JACK_DBUS_SIGNAL_DESCRIBE(PortDisappeared)
  1558. JACK_DBUS_SIGNAL_DESCRIBE(PortsConnected)
  1559. JACK_DBUS_SIGNAL_DESCRIBE(PortsDisconnected)
  1560. JACK_DBUS_SIGNALS_END
  1561. JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_patchbay, JACK_DBUS_IFACE_NAME)
  1562. JACK_DBUS_IFACE_EXPOSE_METHODS
  1563. JACK_DBUS_IFACE_EXPOSE_SIGNALS
  1564. JACK_DBUS_IFACE_END
  1565. /* -*- Mode: C ; c-basic-offset: 4 -*- */
  1566. /*
  1567. Copyright (C) 2008 Nedko Arnaudov
  1568. Copyright (C) 2008 Juuso Alasuutari
  1569. This program is free software; you can redistribute it and/or modify
  1570. it under the terms of the GNU General Public License as published by
  1571. the Free Software Foundation; either version 2 of the License.
  1572. This program is distributed in the hope that it will be useful,
  1573. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1574. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1575. GNU General Public License for more details.
  1576. You should have received a copy of the GNU General Public License
  1577. along with this program; if not, write to the Free Software
  1578. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1579. */
  1580. #define _GNU_SOURCE /* PTHREAD_MUTEX_RECURSIVE */
  1581. #include <stdint.h>
  1582. #include <inttypes.h>
  1583. #include <string.h>
  1584. #include <stdio.h>
  1585. #include <assert.h>
  1586. #include <dbus/dbus.h>
  1587. #include <pthread.h>
  1588. #include "jackdbus.h"
  1589. #include "controller_internal.h"
  1590. #include "list.h"
  1591. #define JACK_DBUS_IFACE_NAME "org.jackaudio.JackPatchbay"
  1592. /* FIXME: these need to be retrieved from common headers */
  1593. #define JACK_CLIENT_NAME_SIZE 64
  1594. #define JACK_PORT_NAME_SIZE 256
  1595. struct jack_graph
  1596. {
  1597. uint64_t version;
  1598. struct list_head clients;
  1599. struct list_head ports;
  1600. struct list_head connections;
  1601. };
  1602. struct jack_graph_client
  1603. {
  1604. uint64_t id;
  1605. char * name;
  1606. int pid;
  1607. struct list_head siblings;
  1608. struct list_head ports;
  1609. };
  1610. struct jack_graph_port
  1611. {
  1612. uint64_t id;
  1613. char * name;
  1614. uint32_t flags;
  1615. uint32_t type;
  1616. struct list_head siblings_graph;
  1617. struct list_head siblings_client;
  1618. struct jack_graph_client * client;
  1619. };
  1620. struct jack_graph_connection
  1621. {
  1622. uint64_t id;
  1623. struct jack_graph_port * port1;
  1624. struct jack_graph_port * port2;
  1625. struct list_head siblings;
  1626. };
  1627. struct jack_controller_patchbay
  1628. {
  1629. pthread_mutex_t lock;
  1630. struct jack_graph graph;
  1631. uint64_t next_client_id;
  1632. uint64_t next_port_id;
  1633. uint64_t next_connection_id;
  1634. };
  1635. void
  1636. jack_controller_patchbay_send_signal_graph_changed(
  1637. dbus_uint64_t new_graph_version)
  1638. {
  1639. jack_dbus_send_signal(
  1640. JACK_CONTROLLER_OBJECT_PATH,
  1641. JACK_DBUS_IFACE_NAME,
  1642. "GraphChanged",
  1643. DBUS_TYPE_UINT64,
  1644. &new_graph_version,
  1645. DBUS_TYPE_INVALID);
  1646. }
  1647. void
  1648. jack_controller_patchbay_send_signal_client_appeared(
  1649. dbus_uint64_t new_graph_version,
  1650. dbus_uint64_t client_id,
  1651. const char * client_name)
  1652. {
  1653. jack_dbus_send_signal(
  1654. JACK_CONTROLLER_OBJECT_PATH,
  1655. JACK_DBUS_IFACE_NAME,
  1656. "ClientAppeared",
  1657. DBUS_TYPE_UINT64,
  1658. &new_graph_version,
  1659. DBUS_TYPE_UINT64,
  1660. &client_id,
  1661. DBUS_TYPE_STRING,
  1662. &client_name,
  1663. DBUS_TYPE_INVALID);
  1664. }
  1665. void
  1666. jack_controller_patchbay_send_signal_client_disappeared(
  1667. dbus_uint64_t new_graph_version,
  1668. dbus_uint64_t client_id,
  1669. const char * client_name)
  1670. {
  1671. jack_dbus_send_signal(
  1672. JACK_CONTROLLER_OBJECT_PATH,
  1673. JACK_DBUS_IFACE_NAME,
  1674. "ClientDisappeared",
  1675. DBUS_TYPE_UINT64,
  1676. &new_graph_version,
  1677. DBUS_TYPE_UINT64,
  1678. &client_id,
  1679. DBUS_TYPE_STRING,
  1680. &client_name,
  1681. DBUS_TYPE_INVALID);
  1682. }
  1683. void
  1684. jack_controller_patchbay_send_signal_port_appeared(
  1685. dbus_uint64_t new_graph_version,
  1686. dbus_uint64_t client_id,
  1687. const char * client_name,
  1688. dbus_uint64_t port_id,
  1689. const char * port_name,
  1690. dbus_uint32_t port_flags,
  1691. dbus_uint32_t port_type)
  1692. {
  1693. jack_dbus_send_signal(
  1694. JACK_CONTROLLER_OBJECT_PATH,
  1695. JACK_DBUS_IFACE_NAME,
  1696. "PortAppeared",
  1697. DBUS_TYPE_UINT64,
  1698. &new_graph_version,
  1699. DBUS_TYPE_UINT64,
  1700. &client_id,
  1701. DBUS_TYPE_STRING,
  1702. &client_name,
  1703. DBUS_TYPE_UINT64,
  1704. &port_id,
  1705. DBUS_TYPE_STRING,
  1706. &port_name,
  1707. DBUS_TYPE_UINT32,
  1708. &port_flags,
  1709. DBUS_TYPE_UINT32,
  1710. &port_type,
  1711. DBUS_TYPE_INVALID);
  1712. }
  1713. void
  1714. jack_controller_patchbay_send_signal_port_disappeared(
  1715. dbus_uint64_t new_graph_version,
  1716. dbus_uint64_t client_id,
  1717. const char * client_name,
  1718. dbus_uint64_t port_id,
  1719. const char * port_name)
  1720. {
  1721. jack_dbus_send_signal(
  1722. JACK_CONTROLLER_OBJECT_PATH,
  1723. JACK_DBUS_IFACE_NAME,
  1724. "PortDisappeared",
  1725. DBUS_TYPE_UINT64,
  1726. &new_graph_version,
  1727. DBUS_TYPE_UINT64,
  1728. &client_id,
  1729. DBUS_TYPE_STRING,
  1730. &client_name,
  1731. DBUS_TYPE_UINT64,
  1732. &port_id,
  1733. DBUS_TYPE_STRING,
  1734. &port_name,
  1735. DBUS_TYPE_INVALID);
  1736. }
  1737. void
  1738. jack_controller_patchbay_send_signal_ports_connected(
  1739. dbus_uint64_t new_graph_version,
  1740. dbus_uint64_t client1_id,
  1741. const char * client1_name,
  1742. dbus_uint64_t port1_id,
  1743. const char * port1_name,
  1744. dbus_uint64_t client2_id,
  1745. const char * client2_name,
  1746. dbus_uint64_t port2_id,
  1747. const char * port2_name,
  1748. dbus_uint64_t connection_id)
  1749. {
  1750. jack_dbus_send_signal(
  1751. JACK_CONTROLLER_OBJECT_PATH,
  1752. JACK_DBUS_IFACE_NAME,
  1753. "PortsConnected",
  1754. DBUS_TYPE_UINT64,
  1755. &new_graph_version,
  1756. DBUS_TYPE_UINT64,
  1757. &client1_id,
  1758. DBUS_TYPE_STRING,
  1759. &client1_name,
  1760. DBUS_TYPE_UINT64,
  1761. &port1_id,
  1762. DBUS_TYPE_STRING,
  1763. &port1_name,
  1764. DBUS_TYPE_UINT64,
  1765. &client2_id,
  1766. DBUS_TYPE_STRING,
  1767. &client2_name,
  1768. DBUS_TYPE_UINT64,
  1769. &port2_id,
  1770. DBUS_TYPE_STRING,
  1771. &port2_name,
  1772. DBUS_TYPE_UINT64,
  1773. &connection_id,
  1774. DBUS_TYPE_INVALID);
  1775. }
  1776. void
  1777. jack_controller_patchbay_send_signal_ports_disconnected(
  1778. dbus_uint64_t new_graph_version,
  1779. dbus_uint64_t client1_id,
  1780. const char * client1_name,
  1781. dbus_uint64_t port1_id,
  1782. const char * port1_name,
  1783. dbus_uint64_t client2_id,
  1784. const char * client2_name,
  1785. dbus_uint64_t port2_id,
  1786. const char * port2_name,
  1787. dbus_uint64_t connection_id)
  1788. {
  1789. jack_dbus_send_signal(
  1790. JACK_CONTROLLER_OBJECT_PATH,
  1791. JACK_DBUS_IFACE_NAME,
  1792. "PortsDisconnected",
  1793. DBUS_TYPE_UINT64,
  1794. &new_graph_version,
  1795. DBUS_TYPE_UINT64,
  1796. &client1_id,
  1797. DBUS_TYPE_STRING,
  1798. &client1_name,
  1799. DBUS_TYPE_UINT64,
  1800. &port1_id,
  1801. DBUS_TYPE_STRING,
  1802. &port1_name,
  1803. DBUS_TYPE_UINT64,
  1804. &client2_id,
  1805. DBUS_TYPE_STRING,
  1806. &client2_name,
  1807. DBUS_TYPE_UINT64,
  1808. &port2_id,
  1809. DBUS_TYPE_STRING,
  1810. &port2_name,
  1811. DBUS_TYPE_UINT64,
  1812. &connection_id,
  1813. DBUS_TYPE_INVALID);
  1814. }
  1815. static
  1816. struct jack_graph_client *
  1817. jack_controller_patchbay_find_client(
  1818. struct jack_controller_patchbay *patchbay_ptr,
  1819. const char *client_name, /* not '\0' terminated */
  1820. size_t client_name_len) /* without terminating '\0' */
  1821. {
  1822. struct list_head *node_ptr;
  1823. struct jack_graph_client *client_ptr;
  1824. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  1825. {
  1826. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  1827. if (strncmp(client_ptr->name, client_name, client_name_len) == 0)
  1828. {
  1829. return client_ptr;
  1830. }
  1831. }
  1832. return NULL;
  1833. }
  1834. static
  1835. struct jack_graph_client *
  1836. jack_controller_patchbay_find_client_by_id(
  1837. struct jack_controller_patchbay *patchbay_ptr,
  1838. uint64_t id)
  1839. {
  1840. struct list_head *node_ptr;
  1841. struct jack_graph_client *client_ptr;
  1842. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  1843. {
  1844. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  1845. if (client_ptr->id == id)
  1846. {
  1847. return client_ptr;
  1848. }
  1849. }
  1850. return NULL;
  1851. }
  1852. static
  1853. struct jack_graph_client *
  1854. jack_controller_patchbay_create_client(
  1855. struct jack_controller_patchbay *patchbay_ptr,
  1856. const char *client_name, /* not '\0' terminated */
  1857. size_t client_name_len) /* without terminating '\0' */
  1858. {
  1859. struct jack_graph_client * client_ptr;
  1860. client_ptr = malloc(sizeof(struct jack_graph_client));
  1861. if (client_ptr == NULL)
  1862. {
  1863. jack_error("Memory allocation of jack_graph_client structure failed.");
  1864. goto fail;
  1865. }
  1866. client_ptr->name = malloc(client_name_len + 1);
  1867. if (client_ptr->name == NULL)
  1868. {
  1869. jack_error("malloc() failed to allocate memory for client name.");
  1870. goto fail_free_client;
  1871. }
  1872. memcpy(client_ptr->name, client_name, client_name_len);
  1873. client_ptr->name[client_name_len] = 0;
  1874. client_ptr->pid = jack_get_client_pid(client_ptr->name);
  1875. jack_info("New client '%s' with PID %d", client_ptr->name, client_ptr->pid);
  1876. client_ptr->id = patchbay_ptr->next_client_id++;
  1877. INIT_LIST_HEAD(&client_ptr->ports);
  1878. pthread_mutex_lock(&patchbay_ptr->lock);
  1879. list_add_tail(&client_ptr->siblings, &patchbay_ptr->graph.clients);
  1880. patchbay_ptr->graph.version++;
  1881. jack_controller_patchbay_send_signal_client_appeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  1882. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  1883. pthread_mutex_unlock(&patchbay_ptr->lock);
  1884. return client_ptr;
  1885. fail_free_client:
  1886. free(client_ptr);
  1887. fail:
  1888. return NULL;
  1889. }
  1890. static
  1891. void
  1892. jack_controller_patchbay_destroy_client(
  1893. struct jack_controller_patchbay *patchbay_ptr,
  1894. struct jack_graph_client *client_ptr)
  1895. {
  1896. pthread_mutex_lock(&patchbay_ptr->lock);
  1897. list_del(&client_ptr->siblings);
  1898. patchbay_ptr->graph.version++;
  1899. jack_controller_patchbay_send_signal_client_disappeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  1900. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  1901. pthread_mutex_unlock(&patchbay_ptr->lock);
  1902. free(client_ptr->name);
  1903. free(client_ptr);
  1904. }
  1905. static
  1906. void
  1907. jack_controller_patchbay_destroy_client_by_name(
  1908. struct jack_controller_patchbay *patchbay_ptr,
  1909. const char *client_name) /* '\0' terminated */
  1910. {
  1911. struct jack_graph_client *client_ptr;
  1912. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  1913. if (client_ptr == NULL)
  1914. {
  1915. jack_error("Cannot destroy unknown client '%s'", client_name);
  1916. return;
  1917. }
  1918. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  1919. }
  1920. static
  1921. void
  1922. jack_controller_patchbay_new_port(
  1923. struct jack_controller_patchbay *patchbay_ptr,
  1924. const char *port_full_name,
  1925. uint32_t port_flags,
  1926. uint32_t port_type)
  1927. {
  1928. struct jack_graph_client *client_ptr;
  1929. struct jack_graph_port *port_ptr;
  1930. const char *port_short_name;
  1931. size_t client_name_len;
  1932. //jack_info("name: %s", port_full_name);
  1933. port_short_name = strchr(port_full_name, ':');
  1934. if (port_short_name == NULL)
  1935. {
  1936. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  1937. return;
  1938. }
  1939. port_short_name++; /* skip ':' separator char */
  1940. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  1941. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  1942. if (client_ptr == NULL)
  1943. {
  1944. client_ptr = jack_controller_patchbay_create_client(patchbay_ptr, port_full_name, client_name_len);
  1945. if (client_ptr == NULL)
  1946. {
  1947. jack_error("Creation of new jack_graph client failed.");
  1948. return;
  1949. }
  1950. }
  1951. port_ptr = malloc(sizeof(struct jack_graph_port));
  1952. if (port_ptr == NULL)
  1953. {
  1954. jack_error("Memory allocation of jack_graph_port structure failed.");
  1955. return;
  1956. }
  1957. port_ptr->name = strdup(port_short_name);
  1958. if (port_ptr->name == NULL)
  1959. {
  1960. jack_error("strdup() call for port name '%s' failed.", port_short_name);
  1961. free(port_ptr);
  1962. return;
  1963. }
  1964. port_ptr->id = patchbay_ptr->next_port_id++;
  1965. port_ptr->flags = port_flags;
  1966. port_ptr->type = port_type;
  1967. port_ptr->client = client_ptr;
  1968. pthread_mutex_lock(&patchbay_ptr->lock);
  1969. list_add_tail(&port_ptr->siblings_client, &client_ptr->ports);
  1970. list_add_tail(&port_ptr->siblings_graph, &patchbay_ptr->graph.ports);
  1971. patchbay_ptr->graph.version++;
  1972. jack_controller_patchbay_send_signal_port_appeared(
  1973. patchbay_ptr->graph.version,
  1974. client_ptr->id,
  1975. client_ptr->name,
  1976. port_ptr->id,
  1977. port_ptr->name,
  1978. port_ptr->flags,
  1979. port_ptr->type);
  1980. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  1981. pthread_mutex_unlock(&patchbay_ptr->lock);
  1982. }
  1983. static
  1984. void
  1985. jack_controller_patchbay_remove_port(
  1986. struct jack_controller_patchbay *patchbay_ptr,
  1987. struct jack_graph_port *port_ptr)
  1988. {
  1989. pthread_mutex_lock(&patchbay_ptr->lock);
  1990. list_del(&port_ptr->siblings_client);
  1991. list_del(&port_ptr->siblings_graph);
  1992. patchbay_ptr->graph.version++;
  1993. 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);
  1994. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  1995. pthread_mutex_unlock(&patchbay_ptr->lock);
  1996. free(port_ptr->name);
  1997. free(port_ptr);
  1998. }
  1999. static
  2000. struct jack_graph_port *
  2001. jack_controller_patchbay_find_port_by_id(
  2002. struct jack_controller_patchbay *patchbay_ptr,
  2003. uint64_t port_id)
  2004. {
  2005. struct list_head *node_ptr;
  2006. struct jack_graph_port *port_ptr;
  2007. list_for_each(node_ptr, &patchbay_ptr->graph.ports)
  2008. {
  2009. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_graph);
  2010. if (port_ptr->id == port_id)
  2011. {
  2012. return port_ptr;
  2013. }
  2014. }
  2015. return NULL;
  2016. }
  2017. static
  2018. struct jack_graph_port *
  2019. jack_controller_patchbay_find_client_port_by_name(
  2020. struct jack_controller_patchbay *patchbay_ptr,
  2021. struct jack_graph_client *client_ptr,
  2022. const char *port_name)
  2023. {
  2024. struct list_head *node_ptr;
  2025. struct jack_graph_port *port_ptr;
  2026. list_for_each(node_ptr, &client_ptr->ports)
  2027. {
  2028. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_client);
  2029. if (strcmp(port_ptr->name, port_name) == 0)
  2030. {
  2031. return port_ptr;
  2032. }
  2033. }
  2034. return NULL;
  2035. }
  2036. static
  2037. struct jack_graph_port *
  2038. jack_controller_patchbay_find_port_by_full_name(
  2039. struct jack_controller_patchbay *patchbay_ptr,
  2040. const char *port_full_name)
  2041. {
  2042. const char *port_short_name;
  2043. size_t client_name_len;
  2044. struct jack_graph_client *client_ptr;
  2045. //jack_info("name: %s", port_full_name);
  2046. port_short_name = strchr(port_full_name, ':');
  2047. if (port_short_name == NULL)
  2048. {
  2049. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  2050. return NULL;
  2051. }
  2052. port_short_name++; /* skip ':' separator char */
  2053. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  2054. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  2055. if (client_ptr == NULL)
  2056. {
  2057. jack_error("cannot find client of port '%s'", port_full_name);
  2058. return NULL;
  2059. }
  2060. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_short_name);
  2061. }
  2062. static
  2063. struct jack_graph_port *
  2064. jack_controller_patchbay_find_port_by_names(
  2065. struct jack_controller_patchbay *patchbay_ptr,
  2066. const char *client_name,
  2067. const char *port_name)
  2068. {
  2069. struct jack_graph_client *client_ptr;
  2070. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  2071. if (client_ptr == NULL)
  2072. {
  2073. jack_error("cannot find client '%s'", client_name);
  2074. return NULL;
  2075. }
  2076. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_name);
  2077. }
  2078. static
  2079. struct jack_graph_connection *
  2080. jack_controller_patchbay_create_connection(
  2081. struct jack_controller_patchbay *patchbay_ptr,
  2082. struct jack_graph_port *port1_ptr,
  2083. struct jack_graph_port *port2_ptr)
  2084. {
  2085. struct jack_graph_connection * connection_ptr;
  2086. connection_ptr = malloc(sizeof(struct jack_graph_connection));
  2087. if (connection_ptr == NULL)
  2088. {
  2089. jack_error("Memory allocation of jack_graph_connection structure failed.");
  2090. return NULL;
  2091. }
  2092. connection_ptr->id = patchbay_ptr->next_connection_id++;
  2093. connection_ptr->port1 = port1_ptr;
  2094. connection_ptr->port2 = port2_ptr;
  2095. pthread_mutex_lock(&patchbay_ptr->lock);
  2096. list_add_tail(&connection_ptr->siblings, &patchbay_ptr->graph.connections);
  2097. patchbay_ptr->graph.version++;
  2098. jack_controller_patchbay_send_signal_ports_connected(
  2099. patchbay_ptr->graph.version,
  2100. port1_ptr->client->id,
  2101. port1_ptr->client->name,
  2102. port1_ptr->id,
  2103. port1_ptr->name,
  2104. port2_ptr->client->id,
  2105. port2_ptr->client->name,
  2106. port2_ptr->id,
  2107. port2_ptr->name,
  2108. connection_ptr->id);
  2109. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  2110. pthread_mutex_unlock(&patchbay_ptr->lock);
  2111. return connection_ptr;
  2112. }
  2113. static
  2114. void
  2115. jack_controller_patchbay_destroy_connection(
  2116. struct jack_controller_patchbay *patchbay_ptr,
  2117. struct jack_graph_connection *connection_ptr)
  2118. {
  2119. pthread_mutex_lock(&patchbay_ptr->lock);
  2120. list_del(&connection_ptr->siblings);
  2121. patchbay_ptr->graph.version++;
  2122. jack_controller_patchbay_send_signal_ports_disconnected(
  2123. patchbay_ptr->graph.version,
  2124. connection_ptr->port1->client->id,
  2125. connection_ptr->port1->client->name,
  2126. connection_ptr->port1->id,
  2127. connection_ptr->port1->name,
  2128. connection_ptr->port2->client->id,
  2129. connection_ptr->port2->client->name,
  2130. connection_ptr->port2->id,
  2131. connection_ptr->port2->name,
  2132. connection_ptr->id);
  2133. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  2134. pthread_mutex_unlock(&patchbay_ptr->lock);
  2135. free(connection_ptr);
  2136. }
  2137. static
  2138. struct jack_graph_connection *
  2139. jack_controller_patchbay_find_connection(
  2140. struct jack_controller_patchbay *patchbay_ptr,
  2141. struct jack_graph_port *port1_ptr,
  2142. struct jack_graph_port *port2_ptr)
  2143. {
  2144. struct list_head *node_ptr;
  2145. struct jack_graph_connection *connection_ptr;
  2146. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  2147. {
  2148. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  2149. if ((connection_ptr->port1 == port1_ptr &&
  2150. connection_ptr->port2 == port2_ptr) ||
  2151. (connection_ptr->port1 == port2_ptr &&
  2152. connection_ptr->port2 == port1_ptr))
  2153. {
  2154. return connection_ptr;
  2155. }
  2156. }
  2157. return NULL;
  2158. }
  2159. static
  2160. struct jack_graph_connection *
  2161. jack_controller_patchbay_find_connection_by_id(
  2162. struct jack_controller_patchbay *patchbay_ptr,
  2163. uint64_t connection_id)
  2164. {
  2165. struct list_head *node_ptr;
  2166. struct jack_graph_connection *connection_ptr;
  2167. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  2168. {
  2169. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  2170. if (connection_ptr->id == connection_id)
  2171. {
  2172. return connection_ptr;
  2173. }
  2174. }
  2175. return NULL;
  2176. }
  2177. static
  2178. bool
  2179. jack_controller_patchbay_connect(
  2180. struct jack_dbus_method_call *dbus_call_ptr,
  2181. struct jack_controller *controller_ptr,
  2182. struct jack_graph_port *port1_ptr,
  2183. struct jack_graph_port *port2_ptr)
  2184. {
  2185. int ret;
  2186. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  2187. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  2188. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  2189. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  2190. ret = jack_connect(controller_ptr->client, port1_name, port2_name);
  2191. if (ret != 0)
  2192. {
  2193. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  2194. return false;
  2195. }
  2196. return true;
  2197. }
  2198. static
  2199. bool
  2200. jack_controller_patchbay_disconnect(
  2201. struct jack_dbus_method_call *dbus_call_ptr,
  2202. struct jack_controller *controller_ptr,
  2203. struct jack_graph_port *port1_ptr,
  2204. struct jack_graph_port *port2_ptr)
  2205. {
  2206. int ret;
  2207. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  2208. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  2209. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  2210. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  2211. ret = jack_disconnect(controller_ptr->client, port1_name, port2_name);
  2212. if (ret != 0)
  2213. {
  2214. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  2215. return false;
  2216. }
  2217. return true;
  2218. }
  2219. #define controller_ptr ((struct jack_controller *)call->context)
  2220. #define patchbay_ptr ((struct jack_controller_patchbay *)controller_ptr->patchbay_context)
  2221. static
  2222. void
  2223. jack_controller_dbus_get_all_ports(
  2224. struct jack_dbus_method_call * call)
  2225. {
  2226. struct list_head * client_node_ptr;
  2227. struct list_head * port_node_ptr;
  2228. struct jack_graph_client * client_ptr;
  2229. struct jack_graph_port * port_ptr;
  2230. DBusMessageIter iter, sub_iter;
  2231. char fullname[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  2232. char *fullname_var = fullname;
  2233. if (!controller_ptr->started)
  2234. {
  2235. jack_dbus_error(
  2236. call,
  2237. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  2238. "Can't execute this method with stopped JACK server");
  2239. return;
  2240. }
  2241. call->reply = dbus_message_new_method_return (call->message);
  2242. if (!call->reply)
  2243. {
  2244. goto fail;
  2245. }
  2246. dbus_message_iter_init_append (call->reply, &iter);
  2247. if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
  2248. {
  2249. goto fail_unref;
  2250. }
  2251. pthread_mutex_lock(&patchbay_ptr->lock);
  2252. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  2253. {
  2254. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  2255. list_for_each(port_node_ptr, &client_ptr->ports)
  2256. {
  2257. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  2258. jack_info("%s:%s", client_ptr->name, port_ptr->name);
  2259. sprintf(fullname, "%s:%s", client_ptr->name, port_ptr->name);
  2260. if (!dbus_message_iter_append_basic (&sub_iter, DBUS_TYPE_STRING, &fullname_var))
  2261. {
  2262. pthread_mutex_unlock(&patchbay_ptr->lock);
  2263. dbus_message_iter_close_container (&iter, &sub_iter);
  2264. goto fail_unref;
  2265. }
  2266. }
  2267. }
  2268. pthread_mutex_unlock(&patchbay_ptr->lock);
  2269. if (!dbus_message_iter_close_container (&iter, &sub_iter))
  2270. {
  2271. goto fail_unref;
  2272. }
  2273. return;
  2274. fail_unref:
  2275. dbus_message_unref (call->reply);
  2276. call->reply = NULL;
  2277. fail:
  2278. jack_error ("Ran out of memory trying to construct method return");
  2279. }
  2280. static
  2281. void
  2282. jack_controller_dbus_get_graph(
  2283. struct jack_dbus_method_call * call)
  2284. {
  2285. struct list_head * client_node_ptr;
  2286. struct list_head * port_node_ptr;
  2287. struct list_head * connection_node_ptr;
  2288. struct jack_graph_client * client_ptr;
  2289. struct jack_graph_port * port_ptr;
  2290. struct jack_graph_connection * connection_ptr;
  2291. DBusMessageIter iter;
  2292. DBusMessageIter clients_array_iter;
  2293. DBusMessageIter client_struct_iter;
  2294. DBusMessageIter ports_array_iter;
  2295. DBusMessageIter port_struct_iter;
  2296. dbus_uint64_t version;
  2297. DBusMessageIter connections_array_iter;
  2298. DBusMessageIter connection_struct_iter;
  2299. if (!controller_ptr->started)
  2300. {
  2301. jack_dbus_error(
  2302. call,
  2303. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  2304. "Can't execute this method with stopped JACK server");
  2305. return;
  2306. }
  2307. if (!jack_dbus_get_method_args(call, DBUS_TYPE_UINT64, &version, DBUS_TYPE_INVALID))
  2308. {
  2309. /* The method call had invalid arguments meaning that
  2310. * jack_dbus_get_method_args() has constructed an error for us.
  2311. */
  2312. goto exit;
  2313. }
  2314. //jack_info("Getting graph, know version is %" PRIu32, version);
  2315. call->reply = dbus_message_new_method_return(call->message);
  2316. if (!call->reply)
  2317. {
  2318. jack_error("Ran out of memory trying to construct method return");
  2319. goto exit;
  2320. }
  2321. dbus_message_iter_init_append (call->reply, &iter);
  2322. pthread_mutex_lock(&patchbay_ptr->lock);
  2323. if (version > patchbay_ptr->graph.version)
  2324. {
  2325. jack_dbus_error(
  2326. call,
  2327. JACK_DBUS_ERROR_INVALID_ARGS,
  2328. "known graph version %" PRIu64 " is newer than actual version %" PRIu64,
  2329. version,
  2330. patchbay_ptr->graph.version);
  2331. pthread_mutex_unlock(&patchbay_ptr->lock);
  2332. goto exit;
  2333. }
  2334. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &patchbay_ptr->graph.version))
  2335. {
  2336. goto nomem_unlock;
  2337. }
  2338. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tsa(tsuu))", &clients_array_iter))
  2339. {
  2340. goto nomem_unlock;
  2341. }
  2342. if (version < patchbay_ptr->graph.version)
  2343. {
  2344. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  2345. {
  2346. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  2347. if (!dbus_message_iter_open_container (&clients_array_iter, DBUS_TYPE_STRUCT, NULL, &client_struct_iter))
  2348. {
  2349. goto nomem_close_clients_array;
  2350. }
  2351. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_UINT64, &client_ptr->id))
  2352. {
  2353. goto nomem_close_client_struct;
  2354. }
  2355. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_STRING, &client_ptr->name))
  2356. {
  2357. goto nomem_close_client_struct;
  2358. }
  2359. if (!dbus_message_iter_open_container(&client_struct_iter, DBUS_TYPE_ARRAY, "(tsuu)", &ports_array_iter))
  2360. {
  2361. goto nomem_close_client_struct;
  2362. }
  2363. list_for_each(port_node_ptr, &client_ptr->ports)
  2364. {
  2365. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  2366. if (!dbus_message_iter_open_container(&ports_array_iter, DBUS_TYPE_STRUCT, NULL, &port_struct_iter))
  2367. {
  2368. goto nomem_close_ports_array;
  2369. }
  2370. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT64, &port_ptr->id))
  2371. {
  2372. goto nomem_close_port_struct;
  2373. }
  2374. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_STRING, &port_ptr->name))
  2375. {
  2376. goto nomem_close_port_struct;
  2377. }
  2378. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->flags))
  2379. {
  2380. goto nomem_close_port_struct;
  2381. }
  2382. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->type))
  2383. {
  2384. goto nomem_close_port_struct;
  2385. }
  2386. if (!dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter))
  2387. {
  2388. goto nomem_close_ports_array;
  2389. }
  2390. }
  2391. if (!dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter))
  2392. {
  2393. goto nomem_close_client_struct;
  2394. }
  2395. if (!dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter))
  2396. {
  2397. goto nomem_close_clients_array;
  2398. }
  2399. }
  2400. }
  2401. if (!dbus_message_iter_close_container(&iter, &clients_array_iter))
  2402. {
  2403. goto nomem_unlock;
  2404. }
  2405. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tstststst)", &connections_array_iter))
  2406. {
  2407. goto nomem_unlock;
  2408. }
  2409. if (version < patchbay_ptr->graph.version)
  2410. {
  2411. list_for_each(connection_node_ptr, &patchbay_ptr->graph.connections)
  2412. {
  2413. connection_ptr = list_entry(connection_node_ptr, struct jack_graph_connection, siblings);
  2414. if (!dbus_message_iter_open_container(&connections_array_iter, DBUS_TYPE_STRUCT, NULL, &connection_struct_iter))
  2415. {
  2416. goto nomem_close_connections_array;
  2417. }
  2418. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->client->id))
  2419. {
  2420. goto nomem_close_connection_struct;
  2421. }
  2422. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->client->name))
  2423. {
  2424. goto nomem_close_connection_struct;
  2425. }
  2426. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->id))
  2427. {
  2428. goto nomem_close_connection_struct;
  2429. }
  2430. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->name))
  2431. {
  2432. goto nomem_close_connection_struct;
  2433. }
  2434. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->client->id))
  2435. {
  2436. goto nomem_close_connection_struct;
  2437. }
  2438. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->client->name))
  2439. {
  2440. goto nomem_close_connection_struct;
  2441. }
  2442. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->id))
  2443. {
  2444. goto nomem_close_connection_struct;
  2445. }
  2446. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->name))
  2447. {
  2448. goto nomem_close_connection_struct;
  2449. }
  2450. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->id))
  2451. {
  2452. goto nomem_close_connection_struct;
  2453. }
  2454. if (!dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter))
  2455. {
  2456. goto nomem_close_connections_array;
  2457. }
  2458. }
  2459. }
  2460. if (!dbus_message_iter_close_container(&iter, &connections_array_iter))
  2461. {
  2462. goto nomem_unlock;
  2463. }
  2464. pthread_mutex_unlock(&patchbay_ptr->lock);
  2465. return;
  2466. nomem_close_connection_struct:
  2467. dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter);
  2468. nomem_close_connections_array:
  2469. dbus_message_iter_close_container(&iter, &connections_array_iter);
  2470. goto nomem_unlock;
  2471. nomem_close_port_struct:
  2472. dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter);
  2473. nomem_close_ports_array:
  2474. dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter);
  2475. nomem_close_client_struct:
  2476. dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter);
  2477. nomem_close_clients_array:
  2478. dbus_message_iter_close_container(&iter, &clients_array_iter);
  2479. nomem_unlock:
  2480. pthread_mutex_unlock(&patchbay_ptr->lock);
  2481. //nomem:
  2482. dbus_message_unref(call->reply);
  2483. call->reply = NULL;
  2484. jack_error("Ran out of memory trying to construct method return");
  2485. exit:
  2486. return;
  2487. }
  2488. static
  2489. void
  2490. jack_controller_dbus_connect_ports_by_name(
  2491. struct jack_dbus_method_call * call)
  2492. {
  2493. const char * client1_name;
  2494. const char * port1_name;
  2495. const char * client2_name;
  2496. const char * port2_name;
  2497. struct jack_graph_port *port1_ptr;
  2498. struct jack_graph_port *port2_ptr;
  2499. /* jack_info("jack_controller_dbus_connect_ports_by_name() called."); */
  2500. if (!controller_ptr->started)
  2501. {
  2502. jack_dbus_error(
  2503. call,
  2504. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  2505. "Can't execute this method with stopped JACK server");
  2506. return;
  2507. }
  2508. if (!jack_dbus_get_method_args(
  2509. call,
  2510. DBUS_TYPE_STRING,
  2511. &client1_name,
  2512. DBUS_TYPE_STRING,
  2513. &port1_name,
  2514. DBUS_TYPE_STRING,
  2515. &client2_name,
  2516. DBUS_TYPE_STRING,
  2517. &port2_name,
  2518. DBUS_TYPE_INVALID))
  2519. {
  2520. /* The method call had invalid arguments meaning that
  2521. * jack_dbus_get_method_args() has constructed an error for us.
  2522. */
  2523. return;
  2524. }
  2525. /* jack_info("connecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  2526. pthread_mutex_lock(&patchbay_ptr->lock);
  2527. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  2528. if (port1_ptr == NULL)
  2529. {
  2530. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  2531. goto unlock;
  2532. }
  2533. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  2534. if (port2_ptr == NULL)
  2535. {
  2536. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  2537. goto unlock;
  2538. }
  2539. if (!jack_controller_patchbay_connect(
  2540. call,
  2541. controller_ptr,
  2542. port1_ptr,
  2543. port2_ptr))
  2544. {
  2545. /* jack_controller_patchbay_connect() constructed error reply */
  2546. goto unlock;
  2547. }
  2548. jack_dbus_construct_method_return_empty(call);
  2549. unlock:
  2550. pthread_mutex_unlock(&patchbay_ptr->lock);
  2551. }
  2552. static
  2553. void
  2554. jack_controller_dbus_connect_ports_by_id(
  2555. struct jack_dbus_method_call * call)
  2556. {
  2557. dbus_uint64_t port1_id;
  2558. dbus_uint64_t port2_id;
  2559. struct jack_graph_port *port1_ptr;
  2560. struct jack_graph_port *port2_ptr;
  2561. /* jack_info("jack_controller_dbus_connect_ports_by_id() called."); */
  2562. if (!controller_ptr->started)
  2563. {
  2564. jack_dbus_error(
  2565. call,
  2566. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  2567. "Can't execute this method with stopped JACK server");
  2568. return;
  2569. }
  2570. if (!jack_dbus_get_method_args(
  2571. call,
  2572. DBUS_TYPE_UINT64,
  2573. &port1_id,
  2574. DBUS_TYPE_UINT64,
  2575. &port2_id,
  2576. DBUS_TYPE_INVALID))
  2577. {
  2578. /* The method call had invalid arguments meaning that
  2579. * jack_dbus_get_method_args() has constructed an error for us.
  2580. */
  2581. return;
  2582. }
  2583. pthread_mutex_lock(&patchbay_ptr->lock);
  2584. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  2585. if (port1_ptr == NULL)
  2586. {
  2587. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  2588. goto unlock;
  2589. }
  2590. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  2591. if (port2_ptr == NULL)
  2592. {
  2593. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  2594. goto unlock;
  2595. }
  2596. if (!jack_controller_patchbay_connect(
  2597. call,
  2598. controller_ptr,
  2599. port1_ptr,
  2600. port2_ptr))
  2601. {
  2602. /* jack_controller_patchbay_connect() constructed error reply */
  2603. goto unlock;
  2604. }
  2605. jack_dbus_construct_method_return_empty(call);
  2606. unlock:
  2607. pthread_mutex_unlock(&patchbay_ptr->lock);
  2608. }
  2609. static
  2610. void
  2611. jack_controller_dbus_disconnect_ports_by_name(
  2612. struct jack_dbus_method_call * call)
  2613. {
  2614. const char * client1_name;
  2615. const char * port1_name;
  2616. const char * client2_name;
  2617. const char * port2_name;
  2618. struct jack_graph_port *port1_ptr;
  2619. struct jack_graph_port *port2_ptr;
  2620. /* jack_info("jack_controller_dbus_disconnect_ports_by_name() called."); */
  2621. if (!controller_ptr->started)
  2622. {
  2623. jack_dbus_error(
  2624. call,
  2625. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  2626. "Can't execute this method with stopped JACK server");
  2627. return;
  2628. }
  2629. if (!jack_dbus_get_method_args(
  2630. call,
  2631. DBUS_TYPE_STRING,
  2632. &client1_name,
  2633. DBUS_TYPE_STRING,
  2634. &port1_name,
  2635. DBUS_TYPE_STRING,
  2636. &client2_name,
  2637. DBUS_TYPE_STRING,
  2638. &port2_name,
  2639. DBUS_TYPE_INVALID))
  2640. {
  2641. /* The method call had invalid arguments meaning that
  2642. * jack_dbus_get_method_args() has constructed an error for us.
  2643. */
  2644. return;
  2645. }
  2646. /* jack_info("disconnecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  2647. pthread_mutex_lock(&patchbay_ptr->lock);
  2648. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  2649. if (port1_ptr == NULL)
  2650. {
  2651. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  2652. goto unlock;
  2653. }
  2654. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  2655. if (port2_ptr == NULL)
  2656. {
  2657. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  2658. goto unlock;
  2659. }
  2660. if (!jack_controller_patchbay_disconnect(
  2661. call,
  2662. controller_ptr,
  2663. port1_ptr,
  2664. port2_ptr))
  2665. {
  2666. /* jack_controller_patchbay_connect() constructed error reply */
  2667. goto unlock;
  2668. }
  2669. jack_dbus_construct_method_return_empty(call);
  2670. unlock:
  2671. pthread_mutex_unlock(&patchbay_ptr->lock);
  2672. }
  2673. static
  2674. void
  2675. jack_controller_dbus_disconnect_ports_by_id(
  2676. struct jack_dbus_method_call * call)
  2677. {
  2678. dbus_uint64_t port1_id;
  2679. dbus_uint64_t port2_id;
  2680. struct jack_graph_port *port1_ptr;
  2681. struct jack_graph_port *port2_ptr;
  2682. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  2683. if (!controller_ptr->started)
  2684. {
  2685. jack_dbus_error(
  2686. call,
  2687. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  2688. "Can't execute this method with stopped JACK server");
  2689. return;
  2690. }
  2691. if (!jack_dbus_get_method_args(
  2692. call,
  2693. DBUS_TYPE_UINT64,
  2694. &port1_id,
  2695. DBUS_TYPE_UINT64,
  2696. &port2_id,
  2697. DBUS_TYPE_INVALID))
  2698. {
  2699. /* The method call had invalid arguments meaning that
  2700. * jack_dbus_get_method_args() has constructed an error for us.
  2701. */
  2702. return;
  2703. }
  2704. pthread_mutex_lock(&patchbay_ptr->lock);
  2705. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  2706. if (port1_ptr == NULL)
  2707. {
  2708. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  2709. return;
  2710. }
  2711. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  2712. if (port2_ptr == NULL)
  2713. {
  2714. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  2715. return;
  2716. }
  2717. if (!jack_controller_patchbay_disconnect(
  2718. call,
  2719. controller_ptr,
  2720. port1_ptr,
  2721. port2_ptr))
  2722. {
  2723. /* jack_controller_patchbay_connect() constructed error reply */
  2724. goto unlock;
  2725. }
  2726. jack_dbus_construct_method_return_empty(call);
  2727. unlock:
  2728. pthread_mutex_unlock(&patchbay_ptr->lock);
  2729. }
  2730. static
  2731. void
  2732. jack_controller_dbus_disconnect_ports_by_connection_id(
  2733. struct jack_dbus_method_call * call)
  2734. {
  2735. dbus_uint64_t connection_id;
  2736. struct jack_graph_connection *connection_ptr;
  2737. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  2738. if (!jack_dbus_get_method_args(
  2739. call,
  2740. DBUS_TYPE_UINT64,
  2741. &connection_id,
  2742. DBUS_TYPE_INVALID))
  2743. {
  2744. /* The method call had invalid arguments meaning that
  2745. * jack_dbus_get_method_args() has constructed an error for us.
  2746. */
  2747. return;
  2748. }
  2749. pthread_mutex_lock(&patchbay_ptr->lock);
  2750. connection_ptr = jack_controller_patchbay_find_connection_by_id(patchbay_ptr, connection_id);
  2751. if (connection_ptr == NULL)
  2752. {
  2753. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find connection %" PRIu64, connection_id);
  2754. goto unlock;
  2755. }
  2756. if (!jack_controller_patchbay_disconnect(
  2757. call,
  2758. controller_ptr,
  2759. connection_ptr->port1,
  2760. connection_ptr->port2))
  2761. {
  2762. /* jack_controller_patchbay_connect() constructed error reply */
  2763. goto unlock;
  2764. }
  2765. jack_dbus_construct_method_return_empty(call);
  2766. unlock:
  2767. pthread_mutex_unlock(&patchbay_ptr->lock);
  2768. }
  2769. static
  2770. void
  2771. jack_controller_dbus_get_client_pid(
  2772. struct jack_dbus_method_call * call)
  2773. {
  2774. dbus_uint64_t client_id;
  2775. struct jack_graph_client *client_ptr;
  2776. message_arg_t arg;
  2777. /* jack_info("jack_controller_dbus_get_client_pid() called."); */
  2778. if (!jack_dbus_get_method_args(
  2779. call,
  2780. DBUS_TYPE_UINT64,
  2781. &client_id,
  2782. DBUS_TYPE_INVALID))
  2783. {
  2784. /* The method call had invalid arguments meaning that
  2785. * jack_dbus_get_method_args() has constructed an error for us.
  2786. */
  2787. return;
  2788. }
  2789. pthread_mutex_lock(&patchbay_ptr->lock);
  2790. client_ptr = jack_controller_patchbay_find_client_by_id(patchbay_ptr, client_id);
  2791. if (client_ptr == NULL)
  2792. {
  2793. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find client %" PRIu64, client_id);
  2794. goto unlock;
  2795. }
  2796. arg.int64 = client_ptr->pid;
  2797. jack_dbus_construct_method_return_single(call, DBUS_TYPE_INT64, arg);
  2798. unlock:
  2799. pthread_mutex_unlock(&patchbay_ptr->lock);
  2800. }
  2801. #undef controller_ptr
  2802. #define controller_ptr ((struct jack_controller *)context)
  2803. static
  2804. int
  2805. jack_controller_graph_order_callback(
  2806. void *context)
  2807. {
  2808. const char **ports;
  2809. int i;
  2810. jack_port_t *port_ptr;
  2811. if (patchbay_ptr->graph.version > 1)
  2812. {
  2813. /* we use this only for initial catchup */
  2814. return 0;
  2815. }
  2816. ports = jack_get_ports(controller_ptr->client, NULL, NULL, 0);
  2817. if (ports)
  2818. {
  2819. for (i = 0; ports[i]; ++i)
  2820. {
  2821. jack_info("graph reorder: new port '%s'", ports[i]);
  2822. port_ptr = jack_port_by_name(controller_ptr->client, ports[i]);;
  2823. jack_controller_patchbay_new_port(patchbay_ptr, ports[i], jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  2824. }
  2825. free(ports);
  2826. }
  2827. if (patchbay_ptr->graph.version == 1)
  2828. {
  2829. /* we have empty initial graph, increment graph version,
  2830. so we dont do jack_get_ports() again,
  2831. on next next graph change */
  2832. patchbay_ptr->graph.version++;
  2833. }
  2834. return 0;
  2835. }
  2836. void
  2837. jack_controller_client_registration_callback(
  2838. const char *client_name,
  2839. int created,
  2840. void *context)
  2841. {
  2842. if (created)
  2843. {
  2844. jack_log("client '%s' created", client_name);
  2845. jack_controller_patchbay_create_client(patchbay_ptr, client_name, strlen(client_name));
  2846. }
  2847. else
  2848. {
  2849. jack_log("client '%s' destroyed", client_name);
  2850. jack_controller_patchbay_destroy_client_by_name(patchbay_ptr, client_name);
  2851. }
  2852. }
  2853. void
  2854. jack_controller_port_registration_callback(
  2855. jack_port_id_t port_id,
  2856. int created,
  2857. void *context)
  2858. {
  2859. jack_port_t *port_ptr;
  2860. struct jack_graph_port *graph_port_ptr;
  2861. const char *port_name;
  2862. port_ptr = jack_port_by_id(controller_ptr->client, port_id);
  2863. port_name = jack_port_name(port_ptr);
  2864. if (created)
  2865. {
  2866. jack_log("port '%s' created", port_name);
  2867. jack_controller_patchbay_new_port(patchbay_ptr, port_name, jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  2868. }
  2869. else
  2870. {
  2871. jack_log("port '%s' destroyed", port_name);
  2872. graph_port_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port_name);
  2873. if (graph_port_ptr == NULL)
  2874. {
  2875. jack_error("Failed to find port '%s' to destroy", port_name);
  2876. return;
  2877. }
  2878. jack_controller_patchbay_remove_port(patchbay_ptr, graph_port_ptr);
  2879. }
  2880. }
  2881. void
  2882. jack_controller_port_connect_callback(
  2883. jack_port_id_t port1_id,
  2884. jack_port_id_t port2_id,
  2885. int connect,
  2886. void *context)
  2887. {
  2888. jack_port_t *port1;
  2889. jack_port_t *port2;
  2890. const char *port1_name;
  2891. const char *port2_name;
  2892. struct jack_graph_port *port1_ptr;
  2893. struct jack_graph_port *port2_ptr;
  2894. struct jack_graph_connection *connection_ptr;
  2895. port1 = jack_port_by_id(controller_ptr->client, port1_id);
  2896. port2 = jack_port_by_id(controller_ptr->client, port2_id);
  2897. port1_name = jack_port_name(port1);
  2898. port2_name = jack_port_name(port2);
  2899. port1_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port1_name);
  2900. if (port1_ptr == NULL)
  2901. {
  2902. jack_error("Failed to find port '%s' to [dis]connect", port1_name);
  2903. return;
  2904. }
  2905. port2_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port2_name);
  2906. if (port2_ptr == NULL)
  2907. {
  2908. jack_error("Failed to find port '%s' to [dis]connect", port2_name);
  2909. return;
  2910. }
  2911. if (connect)
  2912. {
  2913. jack_info("Connecting '%s' to '%s'", port1_name, port2_name);
  2914. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  2915. if (connection_ptr != NULL)
  2916. {
  2917. jack_error("'%s' and '%s' are already connected", port1_name, port2_name);
  2918. return;
  2919. }
  2920. jack_controller_patchbay_create_connection(patchbay_ptr, port1_ptr, port2_ptr);
  2921. }
  2922. else
  2923. {
  2924. jack_info("Disonnecting '%s' from '%s'", port1_name, port2_name);
  2925. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  2926. if (connection_ptr == NULL)
  2927. {
  2928. jack_error("Cannot find connection being removed");
  2929. return;
  2930. }
  2931. jack_controller_patchbay_destroy_connection(patchbay_ptr, connection_ptr);
  2932. }
  2933. }
  2934. #undef controller_ptr
  2935. void
  2936. jack_controller_patchbay_uninit(
  2937. struct jack_controller * controller_ptr)
  2938. {
  2939. struct jack_graph_client *client_ptr;
  2940. struct jack_graph_port *port_ptr;
  2941. /* jack_info("jack_controller_patchbay_uninit() called"); */
  2942. while (!list_empty(&patchbay_ptr->graph.ports))
  2943. {
  2944. port_ptr = list_entry(patchbay_ptr->graph.ports.next, struct jack_graph_port, siblings_graph);
  2945. jack_controller_patchbay_remove_port(patchbay_ptr, port_ptr);
  2946. }
  2947. while (!list_empty(&patchbay_ptr->graph.clients))
  2948. {
  2949. client_ptr = list_entry(patchbay_ptr->graph.clients.next, struct jack_graph_client, siblings);
  2950. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  2951. }
  2952. pthread_mutex_destroy(&patchbay_ptr->lock);
  2953. }
  2954. #undef patchbay_ptr
  2955. bool
  2956. jack_controller_patchbay_init(
  2957. struct jack_controller * controller_ptr)
  2958. {
  2959. int ret;
  2960. struct jack_controller_patchbay * patchbay_ptr;
  2961. pthread_mutexattr_t attr;
  2962. /* jack_info("jack_controller_patchbay_init() called"); */
  2963. patchbay_ptr = malloc(sizeof(struct jack_controller_patchbay));
  2964. if (patchbay_ptr == NULL)
  2965. {
  2966. jack_error("Memory allocation of jack_controller_patchbay structure failed.");
  2967. goto fail;
  2968. }
  2969. ret = pthread_mutexattr_init(&attr);
  2970. if (ret != 0)
  2971. {
  2972. goto fail;
  2973. }
  2974. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  2975. if (ret != 0)
  2976. {
  2977. goto fail;
  2978. }
  2979. pthread_mutex_init(&patchbay_ptr->lock, &attr);
  2980. INIT_LIST_HEAD(&patchbay_ptr->graph.clients);
  2981. INIT_LIST_HEAD(&patchbay_ptr->graph.ports);
  2982. INIT_LIST_HEAD(&patchbay_ptr->graph.connections);
  2983. patchbay_ptr->graph.version = 1;
  2984. patchbay_ptr->next_client_id = 1;
  2985. patchbay_ptr->next_port_id = 1;
  2986. patchbay_ptr->next_connection_id = 1;
  2987. controller_ptr->patchbay_context = patchbay_ptr;
  2988. ret = jack_set_graph_order_callback(controller_ptr->client, jack_controller_graph_order_callback, controller_ptr);
  2989. if (ret != 0)
  2990. {
  2991. jack_error("jack_set_graph_order_callback() failed with error %d", ret);
  2992. goto fail_uninit_mutex;
  2993. }
  2994. ret = jack_set_client_registration_callback(controller_ptr->client, jack_controller_client_registration_callback, controller_ptr);
  2995. if (ret != 0)
  2996. {
  2997. jack_error("jack_set_client_registration_callback() failed with error %d", ret);
  2998. goto fail_uninit_mutex;
  2999. }
  3000. ret = jack_set_port_registration_callback(controller_ptr->client, jack_controller_port_registration_callback, controller_ptr);
  3001. if (ret != 0)
  3002. {
  3003. jack_error("jack_set_port_registration_callback() failed with error %d", ret);
  3004. goto fail_uninit_mutex;
  3005. }
  3006. ret = jack_set_port_connect_callback(controller_ptr->client, jack_controller_port_connect_callback, controller_ptr);
  3007. if (ret != 0)
  3008. {
  3009. jack_error("jack_set_port_connect_callback() failed with error %d", ret);
  3010. goto fail_uninit_mutex;
  3011. }
  3012. return true;
  3013. fail_uninit_mutex:
  3014. pthread_mutex_destroy(&patchbay_ptr->lock);
  3015. fail:
  3016. return false;
  3017. }
  3018. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetAllPorts)
  3019. JACK_DBUS_METHOD_ARGUMENT("ports_list", "as", true)
  3020. JACK_DBUS_METHOD_ARGUMENTS_END
  3021. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetGraph)
  3022. JACK_DBUS_METHOD_ARGUMENT("known_graph_version", DBUS_TYPE_UINT64_AS_STRING, false)
  3023. JACK_DBUS_METHOD_ARGUMENT("current_graph_version", DBUS_TYPE_UINT64_AS_STRING, true)
  3024. JACK_DBUS_METHOD_ARGUMENT("clients_and_ports", "a(tsa(tsuu))", true)
  3025. JACK_DBUS_METHOD_ARGUMENT("connections", "a(tstststst)", true)
  3026. JACK_DBUS_METHOD_ARGUMENTS_END
  3027. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByName)
  3028. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  3029. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  3030. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  3031. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  3032. JACK_DBUS_METHOD_ARGUMENTS_END
  3033. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByID)
  3034. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  3035. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  3036. JACK_DBUS_METHOD_ARGUMENTS_END
  3037. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByName)
  3038. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  3039. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  3040. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  3041. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  3042. JACK_DBUS_METHOD_ARGUMENTS_END
  3043. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByID)
  3044. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  3045. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  3046. JACK_DBUS_METHOD_ARGUMENTS_END
  3047. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByConnectionID)
  3048. JACK_DBUS_METHOD_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING, false)
  3049. JACK_DBUS_METHOD_ARGUMENTS_END
  3050. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetClientPID)
  3051. JACK_DBUS_METHOD_ARGUMENT("client_id", DBUS_TYPE_INT64_AS_STRING, false)
  3052. JACK_DBUS_METHOD_ARGUMENTS_END
  3053. JACK_DBUS_METHODS_BEGIN
  3054. JACK_DBUS_METHOD_DESCRIBE(GetAllPorts, jack_controller_dbus_get_all_ports)
  3055. JACK_DBUS_METHOD_DESCRIBE(GetGraph, jack_controller_dbus_get_graph)
  3056. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByName, jack_controller_dbus_connect_ports_by_name)
  3057. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByID, jack_controller_dbus_connect_ports_by_id)
  3058. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByName, jack_controller_dbus_disconnect_ports_by_name)
  3059. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByID, jack_controller_dbus_disconnect_ports_by_id)
  3060. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByConnectionID, jack_controller_dbus_disconnect_ports_by_connection_id)
  3061. JACK_DBUS_METHOD_DESCRIBE(GetClientPID, jack_controller_dbus_get_client_pid)
  3062. JACK_DBUS_METHODS_END
  3063. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(GraphChanged)
  3064. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  3065. JACK_DBUS_SIGNAL_ARGUMENTS_END
  3066. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientAppeared)
  3067. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  3068. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  3069. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  3070. JACK_DBUS_SIGNAL_ARGUMENTS_END
  3071. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientDisappeared)
  3072. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  3073. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  3074. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  3075. JACK_DBUS_SIGNAL_ARGUMENTS_END
  3076. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortAppeared)
  3077. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  3078. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  3079. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  3080. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  3081. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  3082. JACK_DBUS_SIGNAL_ARGUMENT("port_flags", DBUS_TYPE_UINT32_AS_STRING)
  3083. JACK_DBUS_SIGNAL_ARGUMENT("port_type", DBUS_TYPE_UINT32_AS_STRING)
  3084. JACK_DBUS_SIGNAL_ARGUMENTS_END
  3085. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortDisappeared)
  3086. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  3087. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  3088. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  3089. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  3090. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  3091. JACK_DBUS_SIGNAL_ARGUMENTS_END
  3092. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsConnected)
  3093. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  3094. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  3095. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  3096. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  3097. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  3098. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  3099. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  3100. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  3101. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  3102. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  3103. JACK_DBUS_SIGNAL_ARGUMENTS_END
  3104. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsDisconnected)
  3105. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  3106. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  3107. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  3108. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  3109. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  3110. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  3111. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  3112. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  3113. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  3114. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  3115. JACK_DBUS_SIGNAL_ARGUMENTS_END
  3116. JACK_DBUS_SIGNALS_BEGIN
  3117. JACK_DBUS_SIGNAL_DESCRIBE(GraphChanged)
  3118. JACK_DBUS_SIGNAL_DESCRIBE(ClientAppeared)
  3119. JACK_DBUS_SIGNAL_DESCRIBE(ClientDisappeared)
  3120. JACK_DBUS_SIGNAL_DESCRIBE(PortAppeared)
  3121. JACK_DBUS_SIGNAL_DESCRIBE(PortDisappeared)
  3122. JACK_DBUS_SIGNAL_DESCRIBE(PortsConnected)
  3123. JACK_DBUS_SIGNAL_DESCRIBE(PortsDisconnected)
  3124. JACK_DBUS_SIGNALS_END
  3125. JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_patchbay, JACK_DBUS_IFACE_NAME)
  3126. JACK_DBUS_IFACE_EXPOSE_METHODS
  3127. JACK_DBUS_IFACE_EXPOSE_SIGNALS
  3128. JACK_DBUS_IFACE_END
  3129. /* -*- Mode: C ; c-basic-offset: 4 -*- */
  3130. /*
  3131. Copyright (C) 2008 Nedko Arnaudov
  3132. Copyright (C) 2008 Juuso Alasuutari
  3133. This program is free software; you can redistribute it and/or modify
  3134. it under the terms of the GNU General Public License as published by
  3135. the Free Software Foundation; either version 2 of the License.
  3136. This program is distributed in the hope that it will be useful,
  3137. but WITHOUT ANY WARRANTY; without even the implied warranty of
  3138. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3139. GNU General Public License for more details.
  3140. You should have received a copy of the GNU General Public License
  3141. along with this program; if not, write to the Free Software
  3142. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  3143. */
  3144. #define _GNU_SOURCE /* PTHREAD_MUTEX_RECURSIVE */
  3145. #include <stdint.h>
  3146. #include <inttypes.h>
  3147. #include <string.h>
  3148. #include <stdio.h>
  3149. #include <assert.h>
  3150. #include <dbus/dbus.h>
  3151. #include <pthread.h>
  3152. #include "jackdbus.h"
  3153. #include "controller_internal.h"
  3154. #include "list.h"
  3155. #define JACK_DBUS_IFACE_NAME "org.jackaudio.JackPatchbay"
  3156. /* FIXME: these need to be retrieved from common headers */
  3157. #define JACK_CLIENT_NAME_SIZE 64
  3158. #define JACK_PORT_NAME_SIZE 256
  3159. struct jack_graph
  3160. {
  3161. uint64_t version;
  3162. struct list_head clients;
  3163. struct list_head ports;
  3164. struct list_head connections;
  3165. };
  3166. struct jack_graph_client
  3167. {
  3168. uint64_t id;
  3169. char * name;
  3170. int pid;
  3171. struct list_head siblings;
  3172. struct list_head ports;
  3173. };
  3174. struct jack_graph_port
  3175. {
  3176. uint64_t id;
  3177. char * name;
  3178. uint32_t flags;
  3179. uint32_t type;
  3180. struct list_head siblings_graph;
  3181. struct list_head siblings_client;
  3182. struct jack_graph_client * client;
  3183. };
  3184. struct jack_graph_connection
  3185. {
  3186. uint64_t id;
  3187. struct jack_graph_port * port1;
  3188. struct jack_graph_port * port2;
  3189. struct list_head siblings;
  3190. };
  3191. struct jack_controller_patchbay
  3192. {
  3193. pthread_mutex_t lock;
  3194. struct jack_graph graph;
  3195. uint64_t next_client_id;
  3196. uint64_t next_port_id;
  3197. uint64_t next_connection_id;
  3198. };
  3199. void
  3200. jack_controller_patchbay_send_signal_graph_changed(
  3201. dbus_uint64_t new_graph_version)
  3202. {
  3203. jack_dbus_send_signal(
  3204. JACK_CONTROLLER_OBJECT_PATH,
  3205. JACK_DBUS_IFACE_NAME,
  3206. "GraphChanged",
  3207. DBUS_TYPE_UINT64,
  3208. &new_graph_version,
  3209. DBUS_TYPE_INVALID);
  3210. }
  3211. void
  3212. jack_controller_patchbay_send_signal_client_appeared(
  3213. dbus_uint64_t new_graph_version,
  3214. dbus_uint64_t client_id,
  3215. const char * client_name)
  3216. {
  3217. jack_dbus_send_signal(
  3218. JACK_CONTROLLER_OBJECT_PATH,
  3219. JACK_DBUS_IFACE_NAME,
  3220. "ClientAppeared",
  3221. DBUS_TYPE_UINT64,
  3222. &new_graph_version,
  3223. DBUS_TYPE_UINT64,
  3224. &client_id,
  3225. DBUS_TYPE_STRING,
  3226. &client_name,
  3227. DBUS_TYPE_INVALID);
  3228. }
  3229. void
  3230. jack_controller_patchbay_send_signal_client_disappeared(
  3231. dbus_uint64_t new_graph_version,
  3232. dbus_uint64_t client_id,
  3233. const char * client_name)
  3234. {
  3235. jack_dbus_send_signal(
  3236. JACK_CONTROLLER_OBJECT_PATH,
  3237. JACK_DBUS_IFACE_NAME,
  3238. "ClientDisappeared",
  3239. DBUS_TYPE_UINT64,
  3240. &new_graph_version,
  3241. DBUS_TYPE_UINT64,
  3242. &client_id,
  3243. DBUS_TYPE_STRING,
  3244. &client_name,
  3245. DBUS_TYPE_INVALID);
  3246. }
  3247. void
  3248. jack_controller_patchbay_send_signal_port_appeared(
  3249. dbus_uint64_t new_graph_version,
  3250. dbus_uint64_t client_id,
  3251. const char * client_name,
  3252. dbus_uint64_t port_id,
  3253. const char * port_name,
  3254. dbus_uint32_t port_flags,
  3255. dbus_uint32_t port_type)
  3256. {
  3257. jack_dbus_send_signal(
  3258. JACK_CONTROLLER_OBJECT_PATH,
  3259. JACK_DBUS_IFACE_NAME,
  3260. "PortAppeared",
  3261. DBUS_TYPE_UINT64,
  3262. &new_graph_version,
  3263. DBUS_TYPE_UINT64,
  3264. &client_id,
  3265. DBUS_TYPE_STRING,
  3266. &client_name,
  3267. DBUS_TYPE_UINT64,
  3268. &port_id,
  3269. DBUS_TYPE_STRING,
  3270. &port_name,
  3271. DBUS_TYPE_UINT32,
  3272. &port_flags,
  3273. DBUS_TYPE_UINT32,
  3274. &port_type,
  3275. DBUS_TYPE_INVALID);
  3276. }
  3277. void
  3278. jack_controller_patchbay_send_signal_port_disappeared(
  3279. dbus_uint64_t new_graph_version,
  3280. dbus_uint64_t client_id,
  3281. const char * client_name,
  3282. dbus_uint64_t port_id,
  3283. const char * port_name)
  3284. {
  3285. jack_dbus_send_signal(
  3286. JACK_CONTROLLER_OBJECT_PATH,
  3287. JACK_DBUS_IFACE_NAME,
  3288. "PortDisappeared",
  3289. DBUS_TYPE_UINT64,
  3290. &new_graph_version,
  3291. DBUS_TYPE_UINT64,
  3292. &client_id,
  3293. DBUS_TYPE_STRING,
  3294. &client_name,
  3295. DBUS_TYPE_UINT64,
  3296. &port_id,
  3297. DBUS_TYPE_STRING,
  3298. &port_name,
  3299. DBUS_TYPE_INVALID);
  3300. }
  3301. void
  3302. jack_controller_patchbay_send_signal_ports_connected(
  3303. dbus_uint64_t new_graph_version,
  3304. dbus_uint64_t client1_id,
  3305. const char * client1_name,
  3306. dbus_uint64_t port1_id,
  3307. const char * port1_name,
  3308. dbus_uint64_t client2_id,
  3309. const char * client2_name,
  3310. dbus_uint64_t port2_id,
  3311. const char * port2_name,
  3312. dbus_uint64_t connection_id)
  3313. {
  3314. jack_dbus_send_signal(
  3315. JACK_CONTROLLER_OBJECT_PATH,
  3316. JACK_DBUS_IFACE_NAME,
  3317. "PortsConnected",
  3318. DBUS_TYPE_UINT64,
  3319. &new_graph_version,
  3320. DBUS_TYPE_UINT64,
  3321. &client1_id,
  3322. DBUS_TYPE_STRING,
  3323. &client1_name,
  3324. DBUS_TYPE_UINT64,
  3325. &port1_id,
  3326. DBUS_TYPE_STRING,
  3327. &port1_name,
  3328. DBUS_TYPE_UINT64,
  3329. &client2_id,
  3330. DBUS_TYPE_STRING,
  3331. &client2_name,
  3332. DBUS_TYPE_UINT64,
  3333. &port2_id,
  3334. DBUS_TYPE_STRING,
  3335. &port2_name,
  3336. DBUS_TYPE_UINT64,
  3337. &connection_id,
  3338. DBUS_TYPE_INVALID);
  3339. }
  3340. void
  3341. jack_controller_patchbay_send_signal_ports_disconnected(
  3342. dbus_uint64_t new_graph_version,
  3343. dbus_uint64_t client1_id,
  3344. const char * client1_name,
  3345. dbus_uint64_t port1_id,
  3346. const char * port1_name,
  3347. dbus_uint64_t client2_id,
  3348. const char * client2_name,
  3349. dbus_uint64_t port2_id,
  3350. const char * port2_name,
  3351. dbus_uint64_t connection_id)
  3352. {
  3353. jack_dbus_send_signal(
  3354. JACK_CONTROLLER_OBJECT_PATH,
  3355. JACK_DBUS_IFACE_NAME,
  3356. "PortsDisconnected",
  3357. DBUS_TYPE_UINT64,
  3358. &new_graph_version,
  3359. DBUS_TYPE_UINT64,
  3360. &client1_id,
  3361. DBUS_TYPE_STRING,
  3362. &client1_name,
  3363. DBUS_TYPE_UINT64,
  3364. &port1_id,
  3365. DBUS_TYPE_STRING,
  3366. &port1_name,
  3367. DBUS_TYPE_UINT64,
  3368. &client2_id,
  3369. DBUS_TYPE_STRING,
  3370. &client2_name,
  3371. DBUS_TYPE_UINT64,
  3372. &port2_id,
  3373. DBUS_TYPE_STRING,
  3374. &port2_name,
  3375. DBUS_TYPE_UINT64,
  3376. &connection_id,
  3377. DBUS_TYPE_INVALID);
  3378. }
  3379. static
  3380. struct jack_graph_client *
  3381. jack_controller_patchbay_find_client(
  3382. struct jack_controller_patchbay *patchbay_ptr,
  3383. const char *client_name, /* not '\0' terminated */
  3384. size_t client_name_len) /* without terminating '\0' */
  3385. {
  3386. struct list_head *node_ptr;
  3387. struct jack_graph_client *client_ptr;
  3388. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  3389. {
  3390. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  3391. if (strncmp(client_ptr->name, client_name, client_name_len) == 0)
  3392. {
  3393. return client_ptr;
  3394. }
  3395. }
  3396. return NULL;
  3397. }
  3398. static
  3399. struct jack_graph_client *
  3400. jack_controller_patchbay_find_client_by_id(
  3401. struct jack_controller_patchbay *patchbay_ptr,
  3402. uint64_t id)
  3403. {
  3404. struct list_head *node_ptr;
  3405. struct jack_graph_client *client_ptr;
  3406. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  3407. {
  3408. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  3409. if (client_ptr->id == id)
  3410. {
  3411. return client_ptr;
  3412. }
  3413. }
  3414. return NULL;
  3415. }
  3416. static
  3417. struct jack_graph_client *
  3418. jack_controller_patchbay_create_client(
  3419. struct jack_controller_patchbay *patchbay_ptr,
  3420. const char *client_name, /* not '\0' terminated */
  3421. size_t client_name_len) /* without terminating '\0' */
  3422. {
  3423. struct jack_graph_client * client_ptr;
  3424. client_ptr = malloc(sizeof(struct jack_graph_client));
  3425. if (client_ptr == NULL)
  3426. {
  3427. jack_error("Memory allocation of jack_graph_client structure failed.");
  3428. goto fail;
  3429. }
  3430. client_ptr->name = malloc(client_name_len + 1);
  3431. if (client_ptr->name == NULL)
  3432. {
  3433. jack_error("malloc() failed to allocate memory for client name.");
  3434. goto fail_free_client;
  3435. }
  3436. memcpy(client_ptr->name, client_name, client_name_len);
  3437. client_ptr->name[client_name_len] = 0;
  3438. client_ptr->pid = jack_get_client_pid(client_ptr->name);
  3439. jack_info("New client '%s' with PID %d", client_ptr->name, client_ptr->pid);
  3440. client_ptr->id = patchbay_ptr->next_client_id++;
  3441. INIT_LIST_HEAD(&client_ptr->ports);
  3442. pthread_mutex_lock(&patchbay_ptr->lock);
  3443. list_add_tail(&client_ptr->siblings, &patchbay_ptr->graph.clients);
  3444. patchbay_ptr->graph.version++;
  3445. jack_controller_patchbay_send_signal_client_appeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  3446. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  3447. pthread_mutex_unlock(&patchbay_ptr->lock);
  3448. return client_ptr;
  3449. fail_free_client:
  3450. free(client_ptr);
  3451. fail:
  3452. return NULL;
  3453. }
  3454. static
  3455. void
  3456. jack_controller_patchbay_destroy_client(
  3457. struct jack_controller_patchbay *patchbay_ptr,
  3458. struct jack_graph_client *client_ptr)
  3459. {
  3460. pthread_mutex_lock(&patchbay_ptr->lock);
  3461. list_del(&client_ptr->siblings);
  3462. patchbay_ptr->graph.version++;
  3463. jack_controller_patchbay_send_signal_client_disappeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  3464. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  3465. pthread_mutex_unlock(&patchbay_ptr->lock);
  3466. free(client_ptr->name);
  3467. free(client_ptr);
  3468. }
  3469. static
  3470. void
  3471. jack_controller_patchbay_destroy_client_by_name(
  3472. struct jack_controller_patchbay *patchbay_ptr,
  3473. const char *client_name) /* '\0' terminated */
  3474. {
  3475. struct jack_graph_client *client_ptr;
  3476. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  3477. if (client_ptr == NULL)
  3478. {
  3479. jack_error("Cannot destroy unknown client '%s'", client_name);
  3480. return;
  3481. }
  3482. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  3483. }
  3484. static
  3485. void
  3486. jack_controller_patchbay_new_port(
  3487. struct jack_controller_patchbay *patchbay_ptr,
  3488. const char *port_full_name,
  3489. uint32_t port_flags,
  3490. uint32_t port_type)
  3491. {
  3492. struct jack_graph_client *client_ptr;
  3493. struct jack_graph_port *port_ptr;
  3494. const char *port_short_name;
  3495. size_t client_name_len;
  3496. //jack_info("name: %s", port_full_name);
  3497. port_short_name = strchr(port_full_name, ':');
  3498. if (port_short_name == NULL)
  3499. {
  3500. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  3501. return;
  3502. }
  3503. port_short_name++; /* skip ':' separator char */
  3504. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  3505. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  3506. if (client_ptr == NULL)
  3507. {
  3508. client_ptr = jack_controller_patchbay_create_client(patchbay_ptr, port_full_name, client_name_len);
  3509. if (client_ptr == NULL)
  3510. {
  3511. jack_error("Creation of new jack_graph client failed.");
  3512. return;
  3513. }
  3514. }
  3515. port_ptr = malloc(sizeof(struct jack_graph_port));
  3516. if (port_ptr == NULL)
  3517. {
  3518. jack_error("Memory allocation of jack_graph_port structure failed.");
  3519. return;
  3520. }
  3521. port_ptr->name = strdup(port_short_name);
  3522. if (port_ptr->name == NULL)
  3523. {
  3524. jack_error("strdup() call for port name '%s' failed.", port_short_name);
  3525. free(port_ptr);
  3526. return;
  3527. }
  3528. port_ptr->id = patchbay_ptr->next_port_id++;
  3529. port_ptr->flags = port_flags;
  3530. port_ptr->type = port_type;
  3531. port_ptr->client = client_ptr;
  3532. pthread_mutex_lock(&patchbay_ptr->lock);
  3533. list_add_tail(&port_ptr->siblings_client, &client_ptr->ports);
  3534. list_add_tail(&port_ptr->siblings_graph, &patchbay_ptr->graph.ports);
  3535. patchbay_ptr->graph.version++;
  3536. jack_controller_patchbay_send_signal_port_appeared(
  3537. patchbay_ptr->graph.version,
  3538. client_ptr->id,
  3539. client_ptr->name,
  3540. port_ptr->id,
  3541. port_ptr->name,
  3542. port_ptr->flags,
  3543. port_ptr->type);
  3544. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  3545. pthread_mutex_unlock(&patchbay_ptr->lock);
  3546. }
  3547. static
  3548. void
  3549. jack_controller_patchbay_remove_port(
  3550. struct jack_controller_patchbay *patchbay_ptr,
  3551. struct jack_graph_port *port_ptr)
  3552. {
  3553. pthread_mutex_lock(&patchbay_ptr->lock);
  3554. list_del(&port_ptr->siblings_client);
  3555. list_del(&port_ptr->siblings_graph);
  3556. patchbay_ptr->graph.version++;
  3557. 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);
  3558. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  3559. pthread_mutex_unlock(&patchbay_ptr->lock);
  3560. free(port_ptr->name);
  3561. free(port_ptr);
  3562. }
  3563. static
  3564. struct jack_graph_port *
  3565. jack_controller_patchbay_find_port_by_id(
  3566. struct jack_controller_patchbay *patchbay_ptr,
  3567. uint64_t port_id)
  3568. {
  3569. struct list_head *node_ptr;
  3570. struct jack_graph_port *port_ptr;
  3571. list_for_each(node_ptr, &patchbay_ptr->graph.ports)
  3572. {
  3573. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_graph);
  3574. if (port_ptr->id == port_id)
  3575. {
  3576. return port_ptr;
  3577. }
  3578. }
  3579. return NULL;
  3580. }
  3581. static
  3582. struct jack_graph_port *
  3583. jack_controller_patchbay_find_client_port_by_name(
  3584. struct jack_controller_patchbay *patchbay_ptr,
  3585. struct jack_graph_client *client_ptr,
  3586. const char *port_name)
  3587. {
  3588. struct list_head *node_ptr;
  3589. struct jack_graph_port *port_ptr;
  3590. list_for_each(node_ptr, &client_ptr->ports)
  3591. {
  3592. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_client);
  3593. if (strcmp(port_ptr->name, port_name) == 0)
  3594. {
  3595. return port_ptr;
  3596. }
  3597. }
  3598. return NULL;
  3599. }
  3600. static
  3601. struct jack_graph_port *
  3602. jack_controller_patchbay_find_port_by_full_name(
  3603. struct jack_controller_patchbay *patchbay_ptr,
  3604. const char *port_full_name)
  3605. {
  3606. const char *port_short_name;
  3607. size_t client_name_len;
  3608. struct jack_graph_client *client_ptr;
  3609. //jack_info("name: %s", port_full_name);
  3610. port_short_name = strchr(port_full_name, ':');
  3611. if (port_short_name == NULL)
  3612. {
  3613. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  3614. return NULL;
  3615. }
  3616. port_short_name++; /* skip ':' separator char */
  3617. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  3618. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  3619. if (client_ptr == NULL)
  3620. {
  3621. jack_error("cannot find client of port '%s'", port_full_name);
  3622. return NULL;
  3623. }
  3624. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_short_name);
  3625. }
  3626. static
  3627. struct jack_graph_port *
  3628. jack_controller_patchbay_find_port_by_names(
  3629. struct jack_controller_patchbay *patchbay_ptr,
  3630. const char *client_name,
  3631. const char *port_name)
  3632. {
  3633. struct jack_graph_client *client_ptr;
  3634. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  3635. if (client_ptr == NULL)
  3636. {
  3637. jack_error("cannot find client '%s'", client_name);
  3638. return NULL;
  3639. }
  3640. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_name);
  3641. }
  3642. static
  3643. struct jack_graph_connection *
  3644. jack_controller_patchbay_create_connection(
  3645. struct jack_controller_patchbay *patchbay_ptr,
  3646. struct jack_graph_port *port1_ptr,
  3647. struct jack_graph_port *port2_ptr)
  3648. {
  3649. struct jack_graph_connection * connection_ptr;
  3650. connection_ptr = malloc(sizeof(struct jack_graph_connection));
  3651. if (connection_ptr == NULL)
  3652. {
  3653. jack_error("Memory allocation of jack_graph_connection structure failed.");
  3654. return NULL;
  3655. }
  3656. connection_ptr->id = patchbay_ptr->next_connection_id++;
  3657. connection_ptr->port1 = port1_ptr;
  3658. connection_ptr->port2 = port2_ptr;
  3659. pthread_mutex_lock(&patchbay_ptr->lock);
  3660. list_add_tail(&connection_ptr->siblings, &patchbay_ptr->graph.connections);
  3661. patchbay_ptr->graph.version++;
  3662. jack_controller_patchbay_send_signal_ports_connected(
  3663. patchbay_ptr->graph.version,
  3664. port1_ptr->client->id,
  3665. port1_ptr->client->name,
  3666. port1_ptr->id,
  3667. port1_ptr->name,
  3668. port2_ptr->client->id,
  3669. port2_ptr->client->name,
  3670. port2_ptr->id,
  3671. port2_ptr->name,
  3672. connection_ptr->id);
  3673. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  3674. pthread_mutex_unlock(&patchbay_ptr->lock);
  3675. return connection_ptr;
  3676. }
  3677. static
  3678. void
  3679. jack_controller_patchbay_destroy_connection(
  3680. struct jack_controller_patchbay *patchbay_ptr,
  3681. struct jack_graph_connection *connection_ptr)
  3682. {
  3683. pthread_mutex_lock(&patchbay_ptr->lock);
  3684. list_del(&connection_ptr->siblings);
  3685. patchbay_ptr->graph.version++;
  3686. jack_controller_patchbay_send_signal_ports_disconnected(
  3687. patchbay_ptr->graph.version,
  3688. connection_ptr->port1->client->id,
  3689. connection_ptr->port1->client->name,
  3690. connection_ptr->port1->id,
  3691. connection_ptr->port1->name,
  3692. connection_ptr->port2->client->id,
  3693. connection_ptr->port2->client->name,
  3694. connection_ptr->port2->id,
  3695. connection_ptr->port2->name,
  3696. connection_ptr->id);
  3697. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  3698. pthread_mutex_unlock(&patchbay_ptr->lock);
  3699. free(connection_ptr);
  3700. }
  3701. static
  3702. struct jack_graph_connection *
  3703. jack_controller_patchbay_find_connection(
  3704. struct jack_controller_patchbay *patchbay_ptr,
  3705. struct jack_graph_port *port1_ptr,
  3706. struct jack_graph_port *port2_ptr)
  3707. {
  3708. struct list_head *node_ptr;
  3709. struct jack_graph_connection *connection_ptr;
  3710. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  3711. {
  3712. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  3713. if ((connection_ptr->port1 == port1_ptr &&
  3714. connection_ptr->port2 == port2_ptr) ||
  3715. (connection_ptr->port1 == port2_ptr &&
  3716. connection_ptr->port2 == port1_ptr))
  3717. {
  3718. return connection_ptr;
  3719. }
  3720. }
  3721. return NULL;
  3722. }
  3723. static
  3724. struct jack_graph_connection *
  3725. jack_controller_patchbay_find_connection_by_id(
  3726. struct jack_controller_patchbay *patchbay_ptr,
  3727. uint64_t connection_id)
  3728. {
  3729. struct list_head *node_ptr;
  3730. struct jack_graph_connection *connection_ptr;
  3731. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  3732. {
  3733. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  3734. if (connection_ptr->id == connection_id)
  3735. {
  3736. return connection_ptr;
  3737. }
  3738. }
  3739. return NULL;
  3740. }
  3741. static
  3742. bool
  3743. jack_controller_patchbay_connect(
  3744. struct jack_dbus_method_call *dbus_call_ptr,
  3745. struct jack_controller *controller_ptr,
  3746. struct jack_graph_port *port1_ptr,
  3747. struct jack_graph_port *port2_ptr)
  3748. {
  3749. int ret;
  3750. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  3751. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  3752. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  3753. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  3754. ret = jack_connect(controller_ptr->client, port1_name, port2_name);
  3755. if (ret != 0)
  3756. {
  3757. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  3758. return false;
  3759. }
  3760. return true;
  3761. }
  3762. static
  3763. bool
  3764. jack_controller_patchbay_disconnect(
  3765. struct jack_dbus_method_call *dbus_call_ptr,
  3766. struct jack_controller *controller_ptr,
  3767. struct jack_graph_port *port1_ptr,
  3768. struct jack_graph_port *port2_ptr)
  3769. {
  3770. int ret;
  3771. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  3772. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  3773. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  3774. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  3775. ret = jack_disconnect(controller_ptr->client, port1_name, port2_name);
  3776. if (ret != 0)
  3777. {
  3778. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  3779. return false;
  3780. }
  3781. return true;
  3782. }
  3783. #define controller_ptr ((struct jack_controller *)call->context)
  3784. #define patchbay_ptr ((struct jack_controller_patchbay *)controller_ptr->patchbay_context)
  3785. static
  3786. void
  3787. jack_controller_dbus_get_all_ports(
  3788. struct jack_dbus_method_call * call)
  3789. {
  3790. struct list_head * client_node_ptr;
  3791. struct list_head * port_node_ptr;
  3792. struct jack_graph_client * client_ptr;
  3793. struct jack_graph_port * port_ptr;
  3794. DBusMessageIter iter, sub_iter;
  3795. char fullname[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  3796. char *fullname_var = fullname;
  3797. if (!controller_ptr->started)
  3798. {
  3799. jack_dbus_error(
  3800. call,
  3801. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  3802. "Can't execute this method with stopped JACK server");
  3803. return;
  3804. }
  3805. call->reply = dbus_message_new_method_return (call->message);
  3806. if (!call->reply)
  3807. {
  3808. goto fail;
  3809. }
  3810. dbus_message_iter_init_append (call->reply, &iter);
  3811. if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
  3812. {
  3813. goto fail_unref;
  3814. }
  3815. pthread_mutex_lock(&patchbay_ptr->lock);
  3816. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  3817. {
  3818. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  3819. list_for_each(port_node_ptr, &client_ptr->ports)
  3820. {
  3821. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  3822. jack_info("%s:%s", client_ptr->name, port_ptr->name);
  3823. sprintf(fullname, "%s:%s", client_ptr->name, port_ptr->name);
  3824. if (!dbus_message_iter_append_basic (&sub_iter, DBUS_TYPE_STRING, &fullname_var))
  3825. {
  3826. pthread_mutex_unlock(&patchbay_ptr->lock);
  3827. dbus_message_iter_close_container (&iter, &sub_iter);
  3828. goto fail_unref;
  3829. }
  3830. }
  3831. }
  3832. pthread_mutex_unlock(&patchbay_ptr->lock);
  3833. if (!dbus_message_iter_close_container (&iter, &sub_iter))
  3834. {
  3835. goto fail_unref;
  3836. }
  3837. return;
  3838. fail_unref:
  3839. dbus_message_unref (call->reply);
  3840. call->reply = NULL;
  3841. fail:
  3842. jack_error ("Ran out of memory trying to construct method return");
  3843. }
  3844. static
  3845. void
  3846. jack_controller_dbus_get_graph(
  3847. struct jack_dbus_method_call * call)
  3848. {
  3849. struct list_head * client_node_ptr;
  3850. struct list_head * port_node_ptr;
  3851. struct list_head * connection_node_ptr;
  3852. struct jack_graph_client * client_ptr;
  3853. struct jack_graph_port * port_ptr;
  3854. struct jack_graph_connection * connection_ptr;
  3855. DBusMessageIter iter;
  3856. DBusMessageIter clients_array_iter;
  3857. DBusMessageIter client_struct_iter;
  3858. DBusMessageIter ports_array_iter;
  3859. DBusMessageIter port_struct_iter;
  3860. dbus_uint64_t version;
  3861. DBusMessageIter connections_array_iter;
  3862. DBusMessageIter connection_struct_iter;
  3863. if (!controller_ptr->started)
  3864. {
  3865. jack_dbus_error(
  3866. call,
  3867. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  3868. "Can't execute this method with stopped JACK server");
  3869. return;
  3870. }
  3871. if (!jack_dbus_get_method_args(call, DBUS_TYPE_UINT64, &version, DBUS_TYPE_INVALID))
  3872. {
  3873. /* The method call had invalid arguments meaning that
  3874. * jack_dbus_get_method_args() has constructed an error for us.
  3875. */
  3876. goto exit;
  3877. }
  3878. //jack_info("Getting graph, know version is %" PRIu32, version);
  3879. call->reply = dbus_message_new_method_return(call->message);
  3880. if (!call->reply)
  3881. {
  3882. jack_error("Ran out of memory trying to construct method return");
  3883. goto exit;
  3884. }
  3885. dbus_message_iter_init_append (call->reply, &iter);
  3886. pthread_mutex_lock(&patchbay_ptr->lock);
  3887. if (version > patchbay_ptr->graph.version)
  3888. {
  3889. jack_dbus_error(
  3890. call,
  3891. JACK_DBUS_ERROR_INVALID_ARGS,
  3892. "known graph version %" PRIu64 " is newer than actual version %" PRIu64,
  3893. version,
  3894. patchbay_ptr->graph.version);
  3895. pthread_mutex_unlock(&patchbay_ptr->lock);
  3896. goto exit;
  3897. }
  3898. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &patchbay_ptr->graph.version))
  3899. {
  3900. goto nomem_unlock;
  3901. }
  3902. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tsa(tsuu))", &clients_array_iter))
  3903. {
  3904. goto nomem_unlock;
  3905. }
  3906. if (version < patchbay_ptr->graph.version)
  3907. {
  3908. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  3909. {
  3910. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  3911. if (!dbus_message_iter_open_container (&clients_array_iter, DBUS_TYPE_STRUCT, NULL, &client_struct_iter))
  3912. {
  3913. goto nomem_close_clients_array;
  3914. }
  3915. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_UINT64, &client_ptr->id))
  3916. {
  3917. goto nomem_close_client_struct;
  3918. }
  3919. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_STRING, &client_ptr->name))
  3920. {
  3921. goto nomem_close_client_struct;
  3922. }
  3923. if (!dbus_message_iter_open_container(&client_struct_iter, DBUS_TYPE_ARRAY, "(tsuu)", &ports_array_iter))
  3924. {
  3925. goto nomem_close_client_struct;
  3926. }
  3927. list_for_each(port_node_ptr, &client_ptr->ports)
  3928. {
  3929. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  3930. if (!dbus_message_iter_open_container(&ports_array_iter, DBUS_TYPE_STRUCT, NULL, &port_struct_iter))
  3931. {
  3932. goto nomem_close_ports_array;
  3933. }
  3934. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT64, &port_ptr->id))
  3935. {
  3936. goto nomem_close_port_struct;
  3937. }
  3938. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_STRING, &port_ptr->name))
  3939. {
  3940. goto nomem_close_port_struct;
  3941. }
  3942. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->flags))
  3943. {
  3944. goto nomem_close_port_struct;
  3945. }
  3946. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->type))
  3947. {
  3948. goto nomem_close_port_struct;
  3949. }
  3950. if (!dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter))
  3951. {
  3952. goto nomem_close_ports_array;
  3953. }
  3954. }
  3955. if (!dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter))
  3956. {
  3957. goto nomem_close_client_struct;
  3958. }
  3959. if (!dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter))
  3960. {
  3961. goto nomem_close_clients_array;
  3962. }
  3963. }
  3964. }
  3965. if (!dbus_message_iter_close_container(&iter, &clients_array_iter))
  3966. {
  3967. goto nomem_unlock;
  3968. }
  3969. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tstststst)", &connections_array_iter))
  3970. {
  3971. goto nomem_unlock;
  3972. }
  3973. if (version < patchbay_ptr->graph.version)
  3974. {
  3975. list_for_each(connection_node_ptr, &patchbay_ptr->graph.connections)
  3976. {
  3977. connection_ptr = list_entry(connection_node_ptr, struct jack_graph_connection, siblings);
  3978. if (!dbus_message_iter_open_container(&connections_array_iter, DBUS_TYPE_STRUCT, NULL, &connection_struct_iter))
  3979. {
  3980. goto nomem_close_connections_array;
  3981. }
  3982. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->client->id))
  3983. {
  3984. goto nomem_close_connection_struct;
  3985. }
  3986. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->client->name))
  3987. {
  3988. goto nomem_close_connection_struct;
  3989. }
  3990. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->id))
  3991. {
  3992. goto nomem_close_connection_struct;
  3993. }
  3994. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->name))
  3995. {
  3996. goto nomem_close_connection_struct;
  3997. }
  3998. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->client->id))
  3999. {
  4000. goto nomem_close_connection_struct;
  4001. }
  4002. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->client->name))
  4003. {
  4004. goto nomem_close_connection_struct;
  4005. }
  4006. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->id))
  4007. {
  4008. goto nomem_close_connection_struct;
  4009. }
  4010. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->name))
  4011. {
  4012. goto nomem_close_connection_struct;
  4013. }
  4014. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->id))
  4015. {
  4016. goto nomem_close_connection_struct;
  4017. }
  4018. if (!dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter))
  4019. {
  4020. goto nomem_close_connections_array;
  4021. }
  4022. }
  4023. }
  4024. if (!dbus_message_iter_close_container(&iter, &connections_array_iter))
  4025. {
  4026. goto nomem_unlock;
  4027. }
  4028. pthread_mutex_unlock(&patchbay_ptr->lock);
  4029. return;
  4030. nomem_close_connection_struct:
  4031. dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter);
  4032. nomem_close_connections_array:
  4033. dbus_message_iter_close_container(&iter, &connections_array_iter);
  4034. goto nomem_unlock;
  4035. nomem_close_port_struct:
  4036. dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter);
  4037. nomem_close_ports_array:
  4038. dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter);
  4039. nomem_close_client_struct:
  4040. dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter);
  4041. nomem_close_clients_array:
  4042. dbus_message_iter_close_container(&iter, &clients_array_iter);
  4043. nomem_unlock:
  4044. pthread_mutex_unlock(&patchbay_ptr->lock);
  4045. //nomem:
  4046. dbus_message_unref(call->reply);
  4047. call->reply = NULL;
  4048. jack_error("Ran out of memory trying to construct method return");
  4049. exit:
  4050. return;
  4051. }
  4052. static
  4053. void
  4054. jack_controller_dbus_connect_ports_by_name(
  4055. struct jack_dbus_method_call * call)
  4056. {
  4057. const char * client1_name;
  4058. const char * port1_name;
  4059. const char * client2_name;
  4060. const char * port2_name;
  4061. struct jack_graph_port *port1_ptr;
  4062. struct jack_graph_port *port2_ptr;
  4063. /* jack_info("jack_controller_dbus_connect_ports_by_name() called."); */
  4064. if (!controller_ptr->started)
  4065. {
  4066. jack_dbus_error(
  4067. call,
  4068. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  4069. "Can't execute this method with stopped JACK server");
  4070. return;
  4071. }
  4072. if (!jack_dbus_get_method_args(
  4073. call,
  4074. DBUS_TYPE_STRING,
  4075. &client1_name,
  4076. DBUS_TYPE_STRING,
  4077. &port1_name,
  4078. DBUS_TYPE_STRING,
  4079. &client2_name,
  4080. DBUS_TYPE_STRING,
  4081. &port2_name,
  4082. DBUS_TYPE_INVALID))
  4083. {
  4084. /* The method call had invalid arguments meaning that
  4085. * jack_dbus_get_method_args() has constructed an error for us.
  4086. */
  4087. return;
  4088. }
  4089. /* jack_info("connecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  4090. pthread_mutex_lock(&patchbay_ptr->lock);
  4091. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  4092. if (port1_ptr == NULL)
  4093. {
  4094. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  4095. goto unlock;
  4096. }
  4097. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  4098. if (port2_ptr == NULL)
  4099. {
  4100. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  4101. goto unlock;
  4102. }
  4103. if (!jack_controller_patchbay_connect(
  4104. call,
  4105. controller_ptr,
  4106. port1_ptr,
  4107. port2_ptr))
  4108. {
  4109. /* jack_controller_patchbay_connect() constructed error reply */
  4110. goto unlock;
  4111. }
  4112. jack_dbus_construct_method_return_empty(call);
  4113. unlock:
  4114. pthread_mutex_unlock(&patchbay_ptr->lock);
  4115. }
  4116. static
  4117. void
  4118. jack_controller_dbus_connect_ports_by_id(
  4119. struct jack_dbus_method_call * call)
  4120. {
  4121. dbus_uint64_t port1_id;
  4122. dbus_uint64_t port2_id;
  4123. struct jack_graph_port *port1_ptr;
  4124. struct jack_graph_port *port2_ptr;
  4125. /* jack_info("jack_controller_dbus_connect_ports_by_id() called."); */
  4126. if (!controller_ptr->started)
  4127. {
  4128. jack_dbus_error(
  4129. call,
  4130. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  4131. "Can't execute this method with stopped JACK server");
  4132. return;
  4133. }
  4134. if (!jack_dbus_get_method_args(
  4135. call,
  4136. DBUS_TYPE_UINT64,
  4137. &port1_id,
  4138. DBUS_TYPE_UINT64,
  4139. &port2_id,
  4140. DBUS_TYPE_INVALID))
  4141. {
  4142. /* The method call had invalid arguments meaning that
  4143. * jack_dbus_get_method_args() has constructed an error for us.
  4144. */
  4145. return;
  4146. }
  4147. pthread_mutex_lock(&patchbay_ptr->lock);
  4148. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  4149. if (port1_ptr == NULL)
  4150. {
  4151. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  4152. goto unlock;
  4153. }
  4154. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  4155. if (port2_ptr == NULL)
  4156. {
  4157. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  4158. goto unlock;
  4159. }
  4160. if (!jack_controller_patchbay_connect(
  4161. call,
  4162. controller_ptr,
  4163. port1_ptr,
  4164. port2_ptr))
  4165. {
  4166. /* jack_controller_patchbay_connect() constructed error reply */
  4167. goto unlock;
  4168. }
  4169. jack_dbus_construct_method_return_empty(call);
  4170. unlock:
  4171. pthread_mutex_unlock(&patchbay_ptr->lock);
  4172. }
  4173. static
  4174. void
  4175. jack_controller_dbus_disconnect_ports_by_name(
  4176. struct jack_dbus_method_call * call)
  4177. {
  4178. const char * client1_name;
  4179. const char * port1_name;
  4180. const char * client2_name;
  4181. const char * port2_name;
  4182. struct jack_graph_port *port1_ptr;
  4183. struct jack_graph_port *port2_ptr;
  4184. /* jack_info("jack_controller_dbus_disconnect_ports_by_name() called."); */
  4185. if (!controller_ptr->started)
  4186. {
  4187. jack_dbus_error(
  4188. call,
  4189. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  4190. "Can't execute this method with stopped JACK server");
  4191. return;
  4192. }
  4193. if (!jack_dbus_get_method_args(
  4194. call,
  4195. DBUS_TYPE_STRING,
  4196. &client1_name,
  4197. DBUS_TYPE_STRING,
  4198. &port1_name,
  4199. DBUS_TYPE_STRING,
  4200. &client2_name,
  4201. DBUS_TYPE_STRING,
  4202. &port2_name,
  4203. DBUS_TYPE_INVALID))
  4204. {
  4205. /* The method call had invalid arguments meaning that
  4206. * jack_dbus_get_method_args() has constructed an error for us.
  4207. */
  4208. return;
  4209. }
  4210. /* jack_info("disconnecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  4211. pthread_mutex_lock(&patchbay_ptr->lock);
  4212. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  4213. if (port1_ptr == NULL)
  4214. {
  4215. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  4216. goto unlock;
  4217. }
  4218. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  4219. if (port2_ptr == NULL)
  4220. {
  4221. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  4222. goto unlock;
  4223. }
  4224. if (!jack_controller_patchbay_disconnect(
  4225. call,
  4226. controller_ptr,
  4227. port1_ptr,
  4228. port2_ptr))
  4229. {
  4230. /* jack_controller_patchbay_connect() constructed error reply */
  4231. goto unlock;
  4232. }
  4233. jack_dbus_construct_method_return_empty(call);
  4234. unlock:
  4235. pthread_mutex_unlock(&patchbay_ptr->lock);
  4236. }
  4237. static
  4238. void
  4239. jack_controller_dbus_disconnect_ports_by_id(
  4240. struct jack_dbus_method_call * call)
  4241. {
  4242. dbus_uint64_t port1_id;
  4243. dbus_uint64_t port2_id;
  4244. struct jack_graph_port *port1_ptr;
  4245. struct jack_graph_port *port2_ptr;
  4246. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  4247. if (!controller_ptr->started)
  4248. {
  4249. jack_dbus_error(
  4250. call,
  4251. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  4252. "Can't execute this method with stopped JACK server");
  4253. return;
  4254. }
  4255. if (!jack_dbus_get_method_args(
  4256. call,
  4257. DBUS_TYPE_UINT64,
  4258. &port1_id,
  4259. DBUS_TYPE_UINT64,
  4260. &port2_id,
  4261. DBUS_TYPE_INVALID))
  4262. {
  4263. /* The method call had invalid arguments meaning that
  4264. * jack_dbus_get_method_args() has constructed an error for us.
  4265. */
  4266. return;
  4267. }
  4268. pthread_mutex_lock(&patchbay_ptr->lock);
  4269. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  4270. if (port1_ptr == NULL)
  4271. {
  4272. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  4273. return;
  4274. }
  4275. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  4276. if (port2_ptr == NULL)
  4277. {
  4278. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  4279. return;
  4280. }
  4281. if (!jack_controller_patchbay_disconnect(
  4282. call,
  4283. controller_ptr,
  4284. port1_ptr,
  4285. port2_ptr))
  4286. {
  4287. /* jack_controller_patchbay_connect() constructed error reply */
  4288. goto unlock;
  4289. }
  4290. jack_dbus_construct_method_return_empty(call);
  4291. unlock:
  4292. pthread_mutex_unlock(&patchbay_ptr->lock);
  4293. }
  4294. static
  4295. void
  4296. jack_controller_dbus_disconnect_ports_by_connection_id(
  4297. struct jack_dbus_method_call * call)
  4298. {
  4299. dbus_uint64_t connection_id;
  4300. struct jack_graph_connection *connection_ptr;
  4301. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  4302. if (!jack_dbus_get_method_args(
  4303. call,
  4304. DBUS_TYPE_UINT64,
  4305. &connection_id,
  4306. DBUS_TYPE_INVALID))
  4307. {
  4308. /* The method call had invalid arguments meaning that
  4309. * jack_dbus_get_method_args() has constructed an error for us.
  4310. */
  4311. return;
  4312. }
  4313. pthread_mutex_lock(&patchbay_ptr->lock);
  4314. connection_ptr = jack_controller_patchbay_find_connection_by_id(patchbay_ptr, connection_id);
  4315. if (connection_ptr == NULL)
  4316. {
  4317. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find connection %" PRIu64, connection_id);
  4318. goto unlock;
  4319. }
  4320. if (!jack_controller_patchbay_disconnect(
  4321. call,
  4322. controller_ptr,
  4323. connection_ptr->port1,
  4324. connection_ptr->port2))
  4325. {
  4326. /* jack_controller_patchbay_connect() constructed error reply */
  4327. goto unlock;
  4328. }
  4329. jack_dbus_construct_method_return_empty(call);
  4330. unlock:
  4331. pthread_mutex_unlock(&patchbay_ptr->lock);
  4332. }
  4333. static
  4334. void
  4335. jack_controller_dbus_get_client_pid(
  4336. struct jack_dbus_method_call * call)
  4337. {
  4338. dbus_uint64_t client_id;
  4339. struct jack_graph_client *client_ptr;
  4340. message_arg_t arg;
  4341. /* jack_info("jack_controller_dbus_get_client_pid() called."); */
  4342. if (!jack_dbus_get_method_args(
  4343. call,
  4344. DBUS_TYPE_UINT64,
  4345. &client_id,
  4346. DBUS_TYPE_INVALID))
  4347. {
  4348. /* The method call had invalid arguments meaning that
  4349. * jack_dbus_get_method_args() has constructed an error for us.
  4350. */
  4351. return;
  4352. }
  4353. pthread_mutex_lock(&patchbay_ptr->lock);
  4354. client_ptr = jack_controller_patchbay_find_client_by_id(patchbay_ptr, client_id);
  4355. if (client_ptr == NULL)
  4356. {
  4357. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find client %" PRIu64, client_id);
  4358. goto unlock;
  4359. }
  4360. arg.int64 = client_ptr->pid;
  4361. jack_dbus_construct_method_return_single(call, DBUS_TYPE_INT64, arg);
  4362. unlock:
  4363. pthread_mutex_unlock(&patchbay_ptr->lock);
  4364. }
  4365. #undef controller_ptr
  4366. #define controller_ptr ((struct jack_controller *)context)
  4367. static
  4368. int
  4369. jack_controller_graph_order_callback(
  4370. void *context)
  4371. {
  4372. const char **ports;
  4373. int i;
  4374. jack_port_t *port_ptr;
  4375. if (patchbay_ptr->graph.version > 1)
  4376. {
  4377. /* we use this only for initial catchup */
  4378. return 0;
  4379. }
  4380. ports = jack_get_ports(controller_ptr->client, NULL, NULL, 0);
  4381. if (ports)
  4382. {
  4383. for (i = 0; ports[i]; ++i)
  4384. {
  4385. jack_info("graph reorder: new port '%s'", ports[i]);
  4386. port_ptr = jack_port_by_name(controller_ptr->client, ports[i]);;
  4387. jack_controller_patchbay_new_port(patchbay_ptr, ports[i], jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  4388. }
  4389. free(ports);
  4390. }
  4391. if (patchbay_ptr->graph.version == 1)
  4392. {
  4393. /* we have empty initial graph, increment graph version,
  4394. so we dont do jack_get_ports() again,
  4395. on next next graph change */
  4396. patchbay_ptr->graph.version++;
  4397. }
  4398. return 0;
  4399. }
  4400. void
  4401. jack_controller_client_registration_callback(
  4402. const char *client_name,
  4403. int created,
  4404. void *context)
  4405. {
  4406. if (created)
  4407. {
  4408. jack_log("client '%s' created", client_name);
  4409. jack_controller_patchbay_create_client(patchbay_ptr, client_name, strlen(client_name));
  4410. }
  4411. else
  4412. {
  4413. jack_log("client '%s' destroyed", client_name);
  4414. jack_controller_patchbay_destroy_client_by_name(patchbay_ptr, client_name);
  4415. }
  4416. }
  4417. void
  4418. jack_controller_port_registration_callback(
  4419. jack_port_id_t port_id,
  4420. int created,
  4421. void *context)
  4422. {
  4423. jack_port_t *port_ptr;
  4424. struct jack_graph_port *graph_port_ptr;
  4425. const char *port_name;
  4426. port_ptr = jack_port_by_id(controller_ptr->client, port_id);
  4427. port_name = jack_port_name(port_ptr);
  4428. if (created)
  4429. {
  4430. jack_log("port '%s' created", port_name);
  4431. jack_controller_patchbay_new_port(patchbay_ptr, port_name, jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  4432. }
  4433. else
  4434. {
  4435. jack_log("port '%s' destroyed", port_name);
  4436. graph_port_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port_name);
  4437. if (graph_port_ptr == NULL)
  4438. {
  4439. jack_error("Failed to find port '%s' to destroy", port_name);
  4440. return;
  4441. }
  4442. jack_controller_patchbay_remove_port(patchbay_ptr, graph_port_ptr);
  4443. }
  4444. }
  4445. void
  4446. jack_controller_port_connect_callback(
  4447. jack_port_id_t port1_id,
  4448. jack_port_id_t port2_id,
  4449. int connect,
  4450. void *context)
  4451. {
  4452. jack_port_t *port1;
  4453. jack_port_t *port2;
  4454. const char *port1_name;
  4455. const char *port2_name;
  4456. struct jack_graph_port *port1_ptr;
  4457. struct jack_graph_port *port2_ptr;
  4458. struct jack_graph_connection *connection_ptr;
  4459. port1 = jack_port_by_id(controller_ptr->client, port1_id);
  4460. port2 = jack_port_by_id(controller_ptr->client, port2_id);
  4461. port1_name = jack_port_name(port1);
  4462. port2_name = jack_port_name(port2);
  4463. port1_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port1_name);
  4464. if (port1_ptr == NULL)
  4465. {
  4466. jack_error("Failed to find port '%s' to [dis]connect", port1_name);
  4467. return;
  4468. }
  4469. port2_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port2_name);
  4470. if (port2_ptr == NULL)
  4471. {
  4472. jack_error("Failed to find port '%s' to [dis]connect", port2_name);
  4473. return;
  4474. }
  4475. if (connect)
  4476. {
  4477. jack_info("Connecting '%s' to '%s'", port1_name, port2_name);
  4478. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  4479. if (connection_ptr != NULL)
  4480. {
  4481. jack_error("'%s' and '%s' are already connected", port1_name, port2_name);
  4482. return;
  4483. }
  4484. jack_controller_patchbay_create_connection(patchbay_ptr, port1_ptr, port2_ptr);
  4485. }
  4486. else
  4487. {
  4488. jack_info("Disonnecting '%s' from '%s'", port1_name, port2_name);
  4489. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  4490. if (connection_ptr == NULL)
  4491. {
  4492. jack_error("Cannot find connection being removed");
  4493. return;
  4494. }
  4495. jack_controller_patchbay_destroy_connection(patchbay_ptr, connection_ptr);
  4496. }
  4497. }
  4498. #undef controller_ptr
  4499. void
  4500. jack_controller_patchbay_uninit(
  4501. struct jack_controller * controller_ptr)
  4502. {
  4503. struct jack_graph_client *client_ptr;
  4504. struct jack_graph_port *port_ptr;
  4505. /* jack_info("jack_controller_patchbay_uninit() called"); */
  4506. while (!list_empty(&patchbay_ptr->graph.ports))
  4507. {
  4508. port_ptr = list_entry(patchbay_ptr->graph.ports.next, struct jack_graph_port, siblings_graph);
  4509. jack_controller_patchbay_remove_port(patchbay_ptr, port_ptr);
  4510. }
  4511. while (!list_empty(&patchbay_ptr->graph.clients))
  4512. {
  4513. client_ptr = list_entry(patchbay_ptr->graph.clients.next, struct jack_graph_client, siblings);
  4514. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  4515. }
  4516. pthread_mutex_destroy(&patchbay_ptr->lock);
  4517. }
  4518. #undef patchbay_ptr
  4519. bool
  4520. jack_controller_patchbay_init(
  4521. struct jack_controller * controller_ptr)
  4522. {
  4523. int ret;
  4524. struct jack_controller_patchbay * patchbay_ptr;
  4525. pthread_mutexattr_t attr;
  4526. /* jack_info("jack_controller_patchbay_init() called"); */
  4527. patchbay_ptr = malloc(sizeof(struct jack_controller_patchbay));
  4528. if (patchbay_ptr == NULL)
  4529. {
  4530. jack_error("Memory allocation of jack_controller_patchbay structure failed.");
  4531. goto fail;
  4532. }
  4533. ret = pthread_mutexattr_init(&attr);
  4534. if (ret != 0)
  4535. {
  4536. goto fail;
  4537. }
  4538. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  4539. if (ret != 0)
  4540. {
  4541. goto fail;
  4542. }
  4543. pthread_mutex_init(&patchbay_ptr->lock, &attr);
  4544. INIT_LIST_HEAD(&patchbay_ptr->graph.clients);
  4545. INIT_LIST_HEAD(&patchbay_ptr->graph.ports);
  4546. INIT_LIST_HEAD(&patchbay_ptr->graph.connections);
  4547. patchbay_ptr->graph.version = 1;
  4548. patchbay_ptr->next_client_id = 1;
  4549. patchbay_ptr->next_port_id = 1;
  4550. patchbay_ptr->next_connection_id = 1;
  4551. controller_ptr->patchbay_context = patchbay_ptr;
  4552. ret = jack_set_graph_order_callback(controller_ptr->client, jack_controller_graph_order_callback, controller_ptr);
  4553. if (ret != 0)
  4554. {
  4555. jack_error("jack_set_graph_order_callback() failed with error %d", ret);
  4556. goto fail_uninit_mutex;
  4557. }
  4558. ret = jack_set_client_registration_callback(controller_ptr->client, jack_controller_client_registration_callback, controller_ptr);
  4559. if (ret != 0)
  4560. {
  4561. jack_error("jack_set_client_registration_callback() failed with error %d", ret);
  4562. goto fail_uninit_mutex;
  4563. }
  4564. ret = jack_set_port_registration_callback(controller_ptr->client, jack_controller_port_registration_callback, controller_ptr);
  4565. if (ret != 0)
  4566. {
  4567. jack_error("jack_set_port_registration_callback() failed with error %d", ret);
  4568. goto fail_uninit_mutex;
  4569. }
  4570. ret = jack_set_port_connect_callback(controller_ptr->client, jack_controller_port_connect_callback, controller_ptr);
  4571. if (ret != 0)
  4572. {
  4573. jack_error("jack_set_port_connect_callback() failed with error %d", ret);
  4574. goto fail_uninit_mutex;
  4575. }
  4576. return true;
  4577. fail_uninit_mutex:
  4578. pthread_mutex_destroy(&patchbay_ptr->lock);
  4579. fail:
  4580. return false;
  4581. }
  4582. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetAllPorts)
  4583. JACK_DBUS_METHOD_ARGUMENT("ports_list", "as", true)
  4584. JACK_DBUS_METHOD_ARGUMENTS_END
  4585. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetGraph)
  4586. JACK_DBUS_METHOD_ARGUMENT("known_graph_version", DBUS_TYPE_UINT64_AS_STRING, false)
  4587. JACK_DBUS_METHOD_ARGUMENT("current_graph_version", DBUS_TYPE_UINT64_AS_STRING, true)
  4588. JACK_DBUS_METHOD_ARGUMENT("clients_and_ports", "a(tsa(tsuu))", true)
  4589. JACK_DBUS_METHOD_ARGUMENT("connections", "a(tstststst)", true)
  4590. JACK_DBUS_METHOD_ARGUMENTS_END
  4591. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByName)
  4592. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  4593. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  4594. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  4595. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  4596. JACK_DBUS_METHOD_ARGUMENTS_END
  4597. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByID)
  4598. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  4599. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  4600. JACK_DBUS_METHOD_ARGUMENTS_END
  4601. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByName)
  4602. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  4603. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  4604. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  4605. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  4606. JACK_DBUS_METHOD_ARGUMENTS_END
  4607. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByID)
  4608. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  4609. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  4610. JACK_DBUS_METHOD_ARGUMENTS_END
  4611. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByConnectionID)
  4612. JACK_DBUS_METHOD_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING, false)
  4613. JACK_DBUS_METHOD_ARGUMENTS_END
  4614. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetClientPID)
  4615. JACK_DBUS_METHOD_ARGUMENT("client_id", DBUS_TYPE_INT64_AS_STRING, false)
  4616. JACK_DBUS_METHOD_ARGUMENTS_END
  4617. JACK_DBUS_METHODS_BEGIN
  4618. JACK_DBUS_METHOD_DESCRIBE(GetAllPorts, jack_controller_dbus_get_all_ports)
  4619. JACK_DBUS_METHOD_DESCRIBE(GetGraph, jack_controller_dbus_get_graph)
  4620. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByName, jack_controller_dbus_connect_ports_by_name)
  4621. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByID, jack_controller_dbus_connect_ports_by_id)
  4622. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByName, jack_controller_dbus_disconnect_ports_by_name)
  4623. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByID, jack_controller_dbus_disconnect_ports_by_id)
  4624. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByConnectionID, jack_controller_dbus_disconnect_ports_by_connection_id)
  4625. JACK_DBUS_METHOD_DESCRIBE(GetClientPID, jack_controller_dbus_get_client_pid)
  4626. JACK_DBUS_METHODS_END
  4627. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(GraphChanged)
  4628. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  4629. JACK_DBUS_SIGNAL_ARGUMENTS_END
  4630. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientAppeared)
  4631. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  4632. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  4633. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  4634. JACK_DBUS_SIGNAL_ARGUMENTS_END
  4635. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientDisappeared)
  4636. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  4637. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  4638. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  4639. JACK_DBUS_SIGNAL_ARGUMENTS_END
  4640. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortAppeared)
  4641. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  4642. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  4643. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  4644. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  4645. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  4646. JACK_DBUS_SIGNAL_ARGUMENT("port_flags", DBUS_TYPE_UINT32_AS_STRING)
  4647. JACK_DBUS_SIGNAL_ARGUMENT("port_type", DBUS_TYPE_UINT32_AS_STRING)
  4648. JACK_DBUS_SIGNAL_ARGUMENTS_END
  4649. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortDisappeared)
  4650. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  4651. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  4652. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  4653. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  4654. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  4655. JACK_DBUS_SIGNAL_ARGUMENTS_END
  4656. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsConnected)
  4657. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  4658. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  4659. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  4660. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  4661. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  4662. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  4663. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  4664. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  4665. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  4666. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  4667. JACK_DBUS_SIGNAL_ARGUMENTS_END
  4668. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsDisconnected)
  4669. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  4670. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  4671. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  4672. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  4673. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  4674. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  4675. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  4676. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  4677. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  4678. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  4679. JACK_DBUS_SIGNAL_ARGUMENTS_END
  4680. JACK_DBUS_SIGNALS_BEGIN
  4681. JACK_DBUS_SIGNAL_DESCRIBE(GraphChanged)
  4682. JACK_DBUS_SIGNAL_DESCRIBE(ClientAppeared)
  4683. JACK_DBUS_SIGNAL_DESCRIBE(ClientDisappeared)
  4684. JACK_DBUS_SIGNAL_DESCRIBE(PortAppeared)
  4685. JACK_DBUS_SIGNAL_DESCRIBE(PortDisappeared)
  4686. JACK_DBUS_SIGNAL_DESCRIBE(PortsConnected)
  4687. JACK_DBUS_SIGNAL_DESCRIBE(PortsDisconnected)
  4688. JACK_DBUS_SIGNALS_END
  4689. JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_patchbay, JACK_DBUS_IFACE_NAME)
  4690. JACK_DBUS_IFACE_EXPOSE_METHODS
  4691. JACK_DBUS_IFACE_EXPOSE_SIGNALS
  4692. JACK_DBUS_IFACE_END
  4693. /* -*- Mode: C ; c-basic-offset: 4 -*- */
  4694. /*
  4695. Copyright (C) 2008 Nedko Arnaudov
  4696. Copyright (C) 2008 Juuso Alasuutari
  4697. This program is free software; you can redistribute it and/or modify
  4698. it under the terms of the GNU General Public License as published by
  4699. the Free Software Foundation; either version 2 of the License.
  4700. This program is distributed in the hope that it will be useful,
  4701. but WITHOUT ANY WARRANTY; without even the implied warranty of
  4702. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4703. GNU General Public License for more details.
  4704. You should have received a copy of the GNU General Public License
  4705. along with this program; if not, write to the Free Software
  4706. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  4707. */
  4708. #define _GNU_SOURCE /* PTHREAD_MUTEX_RECURSIVE */
  4709. #include <stdint.h>
  4710. #include <inttypes.h>
  4711. #include <string.h>
  4712. #include <stdio.h>
  4713. #include <assert.h>
  4714. #include <dbus/dbus.h>
  4715. #include <pthread.h>
  4716. #include "jackdbus.h"
  4717. #include "controller_internal.h"
  4718. #include "list.h"
  4719. #define JACK_DBUS_IFACE_NAME "org.jackaudio.JackPatchbay"
  4720. /* FIXME: these need to be retrieved from common headers */
  4721. #define JACK_CLIENT_NAME_SIZE 64
  4722. #define JACK_PORT_NAME_SIZE 256
  4723. struct jack_graph
  4724. {
  4725. uint64_t version;
  4726. struct list_head clients;
  4727. struct list_head ports;
  4728. struct list_head connections;
  4729. };
  4730. struct jack_graph_client
  4731. {
  4732. uint64_t id;
  4733. char * name;
  4734. int pid;
  4735. struct list_head siblings;
  4736. struct list_head ports;
  4737. };
  4738. struct jack_graph_port
  4739. {
  4740. uint64_t id;
  4741. char * name;
  4742. uint32_t flags;
  4743. uint32_t type;
  4744. struct list_head siblings_graph;
  4745. struct list_head siblings_client;
  4746. struct jack_graph_client * client;
  4747. };
  4748. struct jack_graph_connection
  4749. {
  4750. uint64_t id;
  4751. struct jack_graph_port * port1;
  4752. struct jack_graph_port * port2;
  4753. struct list_head siblings;
  4754. };
  4755. struct jack_controller_patchbay
  4756. {
  4757. pthread_mutex_t lock;
  4758. struct jack_graph graph;
  4759. uint64_t next_client_id;
  4760. uint64_t next_port_id;
  4761. uint64_t next_connection_id;
  4762. };
  4763. void
  4764. jack_controller_patchbay_send_signal_graph_changed(
  4765. dbus_uint64_t new_graph_version)
  4766. {
  4767. jack_dbus_send_signal(
  4768. JACK_CONTROLLER_OBJECT_PATH,
  4769. JACK_DBUS_IFACE_NAME,
  4770. "GraphChanged",
  4771. DBUS_TYPE_UINT64,
  4772. &new_graph_version,
  4773. DBUS_TYPE_INVALID);
  4774. }
  4775. void
  4776. jack_controller_patchbay_send_signal_client_appeared(
  4777. dbus_uint64_t new_graph_version,
  4778. dbus_uint64_t client_id,
  4779. const char * client_name)
  4780. {
  4781. jack_dbus_send_signal(
  4782. JACK_CONTROLLER_OBJECT_PATH,
  4783. JACK_DBUS_IFACE_NAME,
  4784. "ClientAppeared",
  4785. DBUS_TYPE_UINT64,
  4786. &new_graph_version,
  4787. DBUS_TYPE_UINT64,
  4788. &client_id,
  4789. DBUS_TYPE_STRING,
  4790. &client_name,
  4791. DBUS_TYPE_INVALID);
  4792. }
  4793. void
  4794. jack_controller_patchbay_send_signal_client_disappeared(
  4795. dbus_uint64_t new_graph_version,
  4796. dbus_uint64_t client_id,
  4797. const char * client_name)
  4798. {
  4799. jack_dbus_send_signal(
  4800. JACK_CONTROLLER_OBJECT_PATH,
  4801. JACK_DBUS_IFACE_NAME,
  4802. "ClientDisappeared",
  4803. DBUS_TYPE_UINT64,
  4804. &new_graph_version,
  4805. DBUS_TYPE_UINT64,
  4806. &client_id,
  4807. DBUS_TYPE_STRING,
  4808. &client_name,
  4809. DBUS_TYPE_INVALID);
  4810. }
  4811. void
  4812. jack_controller_patchbay_send_signal_port_appeared(
  4813. dbus_uint64_t new_graph_version,
  4814. dbus_uint64_t client_id,
  4815. const char * client_name,
  4816. dbus_uint64_t port_id,
  4817. const char * port_name,
  4818. dbus_uint32_t port_flags,
  4819. dbus_uint32_t port_type)
  4820. {
  4821. jack_dbus_send_signal(
  4822. JACK_CONTROLLER_OBJECT_PATH,
  4823. JACK_DBUS_IFACE_NAME,
  4824. "PortAppeared",
  4825. DBUS_TYPE_UINT64,
  4826. &new_graph_version,
  4827. DBUS_TYPE_UINT64,
  4828. &client_id,
  4829. DBUS_TYPE_STRING,
  4830. &client_name,
  4831. DBUS_TYPE_UINT64,
  4832. &port_id,
  4833. DBUS_TYPE_STRING,
  4834. &port_name,
  4835. DBUS_TYPE_UINT32,
  4836. &port_flags,
  4837. DBUS_TYPE_UINT32,
  4838. &port_type,
  4839. DBUS_TYPE_INVALID);
  4840. }
  4841. void
  4842. jack_controller_patchbay_send_signal_port_disappeared(
  4843. dbus_uint64_t new_graph_version,
  4844. dbus_uint64_t client_id,
  4845. const char * client_name,
  4846. dbus_uint64_t port_id,
  4847. const char * port_name)
  4848. {
  4849. jack_dbus_send_signal(
  4850. JACK_CONTROLLER_OBJECT_PATH,
  4851. JACK_DBUS_IFACE_NAME,
  4852. "PortDisappeared",
  4853. DBUS_TYPE_UINT64,
  4854. &new_graph_version,
  4855. DBUS_TYPE_UINT64,
  4856. &client_id,
  4857. DBUS_TYPE_STRING,
  4858. &client_name,
  4859. DBUS_TYPE_UINT64,
  4860. &port_id,
  4861. DBUS_TYPE_STRING,
  4862. &port_name,
  4863. DBUS_TYPE_INVALID);
  4864. }
  4865. void
  4866. jack_controller_patchbay_send_signal_ports_connected(
  4867. dbus_uint64_t new_graph_version,
  4868. dbus_uint64_t client1_id,
  4869. const char * client1_name,
  4870. dbus_uint64_t port1_id,
  4871. const char * port1_name,
  4872. dbus_uint64_t client2_id,
  4873. const char * client2_name,
  4874. dbus_uint64_t port2_id,
  4875. const char * port2_name,
  4876. dbus_uint64_t connection_id)
  4877. {
  4878. jack_dbus_send_signal(
  4879. JACK_CONTROLLER_OBJECT_PATH,
  4880. JACK_DBUS_IFACE_NAME,
  4881. "PortsConnected",
  4882. DBUS_TYPE_UINT64,
  4883. &new_graph_version,
  4884. DBUS_TYPE_UINT64,
  4885. &client1_id,
  4886. DBUS_TYPE_STRING,
  4887. &client1_name,
  4888. DBUS_TYPE_UINT64,
  4889. &port1_id,
  4890. DBUS_TYPE_STRING,
  4891. &port1_name,
  4892. DBUS_TYPE_UINT64,
  4893. &client2_id,
  4894. DBUS_TYPE_STRING,
  4895. &client2_name,
  4896. DBUS_TYPE_UINT64,
  4897. &port2_id,
  4898. DBUS_TYPE_STRING,
  4899. &port2_name,
  4900. DBUS_TYPE_UINT64,
  4901. &connection_id,
  4902. DBUS_TYPE_INVALID);
  4903. }
  4904. void
  4905. jack_controller_patchbay_send_signal_ports_disconnected(
  4906. dbus_uint64_t new_graph_version,
  4907. dbus_uint64_t client1_id,
  4908. const char * client1_name,
  4909. dbus_uint64_t port1_id,
  4910. const char * port1_name,
  4911. dbus_uint64_t client2_id,
  4912. const char * client2_name,
  4913. dbus_uint64_t port2_id,
  4914. const char * port2_name,
  4915. dbus_uint64_t connection_id)
  4916. {
  4917. jack_dbus_send_signal(
  4918. JACK_CONTROLLER_OBJECT_PATH,
  4919. JACK_DBUS_IFACE_NAME,
  4920. "PortsDisconnected",
  4921. DBUS_TYPE_UINT64,
  4922. &new_graph_version,
  4923. DBUS_TYPE_UINT64,
  4924. &client1_id,
  4925. DBUS_TYPE_STRING,
  4926. &client1_name,
  4927. DBUS_TYPE_UINT64,
  4928. &port1_id,
  4929. DBUS_TYPE_STRING,
  4930. &port1_name,
  4931. DBUS_TYPE_UINT64,
  4932. &client2_id,
  4933. DBUS_TYPE_STRING,
  4934. &client2_name,
  4935. DBUS_TYPE_UINT64,
  4936. &port2_id,
  4937. DBUS_TYPE_STRING,
  4938. &port2_name,
  4939. DBUS_TYPE_UINT64,
  4940. &connection_id,
  4941. DBUS_TYPE_INVALID);
  4942. }
  4943. static
  4944. struct jack_graph_client *
  4945. jack_controller_patchbay_find_client(
  4946. struct jack_controller_patchbay *patchbay_ptr,
  4947. const char *client_name, /* not '\0' terminated */
  4948. size_t client_name_len) /* without terminating '\0' */
  4949. {
  4950. struct list_head *node_ptr;
  4951. struct jack_graph_client *client_ptr;
  4952. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  4953. {
  4954. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  4955. if (strncmp(client_ptr->name, client_name, client_name_len) == 0)
  4956. {
  4957. return client_ptr;
  4958. }
  4959. }
  4960. return NULL;
  4961. }
  4962. static
  4963. struct jack_graph_client *
  4964. jack_controller_patchbay_find_client_by_id(
  4965. struct jack_controller_patchbay *patchbay_ptr,
  4966. uint64_t id)
  4967. {
  4968. struct list_head *node_ptr;
  4969. struct jack_graph_client *client_ptr;
  4970. list_for_each(node_ptr, &patchbay_ptr->graph.clients)
  4971. {
  4972. client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
  4973. if (client_ptr->id == id)
  4974. {
  4975. return client_ptr;
  4976. }
  4977. }
  4978. return NULL;
  4979. }
  4980. static
  4981. struct jack_graph_client *
  4982. jack_controller_patchbay_create_client(
  4983. struct jack_controller_patchbay *patchbay_ptr,
  4984. const char *client_name, /* not '\0' terminated */
  4985. size_t client_name_len) /* without terminating '\0' */
  4986. {
  4987. struct jack_graph_client * client_ptr;
  4988. client_ptr = malloc(sizeof(struct jack_graph_client));
  4989. if (client_ptr == NULL)
  4990. {
  4991. jack_error("Memory allocation of jack_graph_client structure failed.");
  4992. goto fail;
  4993. }
  4994. client_ptr->name = malloc(client_name_len + 1);
  4995. if (client_ptr->name == NULL)
  4996. {
  4997. jack_error("malloc() failed to allocate memory for client name.");
  4998. goto fail_free_client;
  4999. }
  5000. memcpy(client_ptr->name, client_name, client_name_len);
  5001. client_ptr->name[client_name_len] = 0;
  5002. client_ptr->pid = jack_get_client_pid(client_ptr->name);
  5003. jack_info("New client '%s' with PID %d", client_ptr->name, client_ptr->pid);
  5004. client_ptr->id = patchbay_ptr->next_client_id++;
  5005. INIT_LIST_HEAD(&client_ptr->ports);
  5006. pthread_mutex_lock(&patchbay_ptr->lock);
  5007. list_add_tail(&client_ptr->siblings, &patchbay_ptr->graph.clients);
  5008. patchbay_ptr->graph.version++;
  5009. jack_controller_patchbay_send_signal_client_appeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  5010. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  5011. pthread_mutex_unlock(&patchbay_ptr->lock);
  5012. return client_ptr;
  5013. fail_free_client:
  5014. free(client_ptr);
  5015. fail:
  5016. return NULL;
  5017. }
  5018. static
  5019. void
  5020. jack_controller_patchbay_destroy_client(
  5021. struct jack_controller_patchbay *patchbay_ptr,
  5022. struct jack_graph_client *client_ptr)
  5023. {
  5024. pthread_mutex_lock(&patchbay_ptr->lock);
  5025. list_del(&client_ptr->siblings);
  5026. patchbay_ptr->graph.version++;
  5027. jack_controller_patchbay_send_signal_client_disappeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
  5028. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  5029. pthread_mutex_unlock(&patchbay_ptr->lock);
  5030. free(client_ptr->name);
  5031. free(client_ptr);
  5032. }
  5033. static
  5034. void
  5035. jack_controller_patchbay_destroy_client_by_name(
  5036. struct jack_controller_patchbay *patchbay_ptr,
  5037. const char *client_name) /* '\0' terminated */
  5038. {
  5039. struct jack_graph_client *client_ptr;
  5040. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  5041. if (client_ptr == NULL)
  5042. {
  5043. jack_error("Cannot destroy unknown client '%s'", client_name);
  5044. return;
  5045. }
  5046. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  5047. }
  5048. static
  5049. void
  5050. jack_controller_patchbay_new_port(
  5051. struct jack_controller_patchbay *patchbay_ptr,
  5052. const char *port_full_name,
  5053. uint32_t port_flags,
  5054. uint32_t port_type)
  5055. {
  5056. struct jack_graph_client *client_ptr;
  5057. struct jack_graph_port *port_ptr;
  5058. const char *port_short_name;
  5059. size_t client_name_len;
  5060. //jack_info("name: %s", port_full_name);
  5061. port_short_name = strchr(port_full_name, ':');
  5062. if (port_short_name == NULL)
  5063. {
  5064. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  5065. return;
  5066. }
  5067. port_short_name++; /* skip ':' separator char */
  5068. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  5069. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  5070. if (client_ptr == NULL)
  5071. {
  5072. client_ptr = jack_controller_patchbay_create_client(patchbay_ptr, port_full_name, client_name_len);
  5073. if (client_ptr == NULL)
  5074. {
  5075. jack_error("Creation of new jack_graph client failed.");
  5076. return;
  5077. }
  5078. }
  5079. port_ptr = malloc(sizeof(struct jack_graph_port));
  5080. if (port_ptr == NULL)
  5081. {
  5082. jack_error("Memory allocation of jack_graph_port structure failed.");
  5083. return;
  5084. }
  5085. port_ptr->name = strdup(port_short_name);
  5086. if (port_ptr->name == NULL)
  5087. {
  5088. jack_error("strdup() call for port name '%s' failed.", port_short_name);
  5089. free(port_ptr);
  5090. return;
  5091. }
  5092. port_ptr->id = patchbay_ptr->next_port_id++;
  5093. port_ptr->flags = port_flags;
  5094. port_ptr->type = port_type;
  5095. port_ptr->client = client_ptr;
  5096. pthread_mutex_lock(&patchbay_ptr->lock);
  5097. list_add_tail(&port_ptr->siblings_client, &client_ptr->ports);
  5098. list_add_tail(&port_ptr->siblings_graph, &patchbay_ptr->graph.ports);
  5099. patchbay_ptr->graph.version++;
  5100. jack_controller_patchbay_send_signal_port_appeared(
  5101. patchbay_ptr->graph.version,
  5102. client_ptr->id,
  5103. client_ptr->name,
  5104. port_ptr->id,
  5105. port_ptr->name,
  5106. port_ptr->flags,
  5107. port_ptr->type);
  5108. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  5109. pthread_mutex_unlock(&patchbay_ptr->lock);
  5110. }
  5111. static
  5112. void
  5113. jack_controller_patchbay_remove_port(
  5114. struct jack_controller_patchbay *patchbay_ptr,
  5115. struct jack_graph_port *port_ptr)
  5116. {
  5117. pthread_mutex_lock(&patchbay_ptr->lock);
  5118. list_del(&port_ptr->siblings_client);
  5119. list_del(&port_ptr->siblings_graph);
  5120. patchbay_ptr->graph.version++;
  5121. 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);
  5122. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  5123. pthread_mutex_unlock(&patchbay_ptr->lock);
  5124. free(port_ptr->name);
  5125. free(port_ptr);
  5126. }
  5127. static
  5128. struct jack_graph_port *
  5129. jack_controller_patchbay_find_port_by_id(
  5130. struct jack_controller_patchbay *patchbay_ptr,
  5131. uint64_t port_id)
  5132. {
  5133. struct list_head *node_ptr;
  5134. struct jack_graph_port *port_ptr;
  5135. list_for_each(node_ptr, &patchbay_ptr->graph.ports)
  5136. {
  5137. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_graph);
  5138. if (port_ptr->id == port_id)
  5139. {
  5140. return port_ptr;
  5141. }
  5142. }
  5143. return NULL;
  5144. }
  5145. static
  5146. struct jack_graph_port *
  5147. jack_controller_patchbay_find_client_port_by_name(
  5148. struct jack_controller_patchbay *patchbay_ptr,
  5149. struct jack_graph_client *client_ptr,
  5150. const char *port_name)
  5151. {
  5152. struct list_head *node_ptr;
  5153. struct jack_graph_port *port_ptr;
  5154. list_for_each(node_ptr, &client_ptr->ports)
  5155. {
  5156. port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_client);
  5157. if (strcmp(port_ptr->name, port_name) == 0)
  5158. {
  5159. return port_ptr;
  5160. }
  5161. }
  5162. return NULL;
  5163. }
  5164. static
  5165. struct jack_graph_port *
  5166. jack_controller_patchbay_find_port_by_full_name(
  5167. struct jack_controller_patchbay *patchbay_ptr,
  5168. const char *port_full_name)
  5169. {
  5170. const char *port_short_name;
  5171. size_t client_name_len;
  5172. struct jack_graph_client *client_ptr;
  5173. //jack_info("name: %s", port_full_name);
  5174. port_short_name = strchr(port_full_name, ':');
  5175. if (port_short_name == NULL)
  5176. {
  5177. jack_error("port name '%s' does not contain ':' separator char", port_full_name);
  5178. return NULL;
  5179. }
  5180. port_short_name++; /* skip ':' separator char */
  5181. client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */
  5182. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
  5183. if (client_ptr == NULL)
  5184. {
  5185. jack_error("cannot find client of port '%s'", port_full_name);
  5186. return NULL;
  5187. }
  5188. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_short_name);
  5189. }
  5190. static
  5191. struct jack_graph_port *
  5192. jack_controller_patchbay_find_port_by_names(
  5193. struct jack_controller_patchbay *patchbay_ptr,
  5194. const char *client_name,
  5195. const char *port_name)
  5196. {
  5197. struct jack_graph_client *client_ptr;
  5198. client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
  5199. if (client_ptr == NULL)
  5200. {
  5201. jack_error("cannot find client '%s'", client_name);
  5202. return NULL;
  5203. }
  5204. return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_name);
  5205. }
  5206. static
  5207. struct jack_graph_connection *
  5208. jack_controller_patchbay_create_connection(
  5209. struct jack_controller_patchbay *patchbay_ptr,
  5210. struct jack_graph_port *port1_ptr,
  5211. struct jack_graph_port *port2_ptr)
  5212. {
  5213. struct jack_graph_connection * connection_ptr;
  5214. connection_ptr = malloc(sizeof(struct jack_graph_connection));
  5215. if (connection_ptr == NULL)
  5216. {
  5217. jack_error("Memory allocation of jack_graph_connection structure failed.");
  5218. return NULL;
  5219. }
  5220. connection_ptr->id = patchbay_ptr->next_connection_id++;
  5221. connection_ptr->port1 = port1_ptr;
  5222. connection_ptr->port2 = port2_ptr;
  5223. pthread_mutex_lock(&patchbay_ptr->lock);
  5224. list_add_tail(&connection_ptr->siblings, &patchbay_ptr->graph.connections);
  5225. patchbay_ptr->graph.version++;
  5226. jack_controller_patchbay_send_signal_ports_connected(
  5227. patchbay_ptr->graph.version,
  5228. port1_ptr->client->id,
  5229. port1_ptr->client->name,
  5230. port1_ptr->id,
  5231. port1_ptr->name,
  5232. port2_ptr->client->id,
  5233. port2_ptr->client->name,
  5234. port2_ptr->id,
  5235. port2_ptr->name,
  5236. connection_ptr->id);
  5237. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  5238. pthread_mutex_unlock(&patchbay_ptr->lock);
  5239. return connection_ptr;
  5240. }
  5241. static
  5242. void
  5243. jack_controller_patchbay_destroy_connection(
  5244. struct jack_controller_patchbay *patchbay_ptr,
  5245. struct jack_graph_connection *connection_ptr)
  5246. {
  5247. pthread_mutex_lock(&patchbay_ptr->lock);
  5248. list_del(&connection_ptr->siblings);
  5249. patchbay_ptr->graph.version++;
  5250. jack_controller_patchbay_send_signal_ports_disconnected(
  5251. patchbay_ptr->graph.version,
  5252. connection_ptr->port1->client->id,
  5253. connection_ptr->port1->client->name,
  5254. connection_ptr->port1->id,
  5255. connection_ptr->port1->name,
  5256. connection_ptr->port2->client->id,
  5257. connection_ptr->port2->client->name,
  5258. connection_ptr->port2->id,
  5259. connection_ptr->port2->name,
  5260. connection_ptr->id);
  5261. jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
  5262. pthread_mutex_unlock(&patchbay_ptr->lock);
  5263. free(connection_ptr);
  5264. }
  5265. static
  5266. struct jack_graph_connection *
  5267. jack_controller_patchbay_find_connection(
  5268. struct jack_controller_patchbay *patchbay_ptr,
  5269. struct jack_graph_port *port1_ptr,
  5270. struct jack_graph_port *port2_ptr)
  5271. {
  5272. struct list_head *node_ptr;
  5273. struct jack_graph_connection *connection_ptr;
  5274. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  5275. {
  5276. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  5277. if ((connection_ptr->port1 == port1_ptr &&
  5278. connection_ptr->port2 == port2_ptr) ||
  5279. (connection_ptr->port1 == port2_ptr &&
  5280. connection_ptr->port2 == port1_ptr))
  5281. {
  5282. return connection_ptr;
  5283. }
  5284. }
  5285. return NULL;
  5286. }
  5287. static
  5288. struct jack_graph_connection *
  5289. jack_controller_patchbay_find_connection_by_id(
  5290. struct jack_controller_patchbay *patchbay_ptr,
  5291. uint64_t connection_id)
  5292. {
  5293. struct list_head *node_ptr;
  5294. struct jack_graph_connection *connection_ptr;
  5295. list_for_each(node_ptr, &patchbay_ptr->graph.connections)
  5296. {
  5297. connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
  5298. if (connection_ptr->id == connection_id)
  5299. {
  5300. return connection_ptr;
  5301. }
  5302. }
  5303. return NULL;
  5304. }
  5305. static
  5306. bool
  5307. jack_controller_patchbay_connect(
  5308. struct jack_dbus_method_call *dbus_call_ptr,
  5309. struct jack_controller *controller_ptr,
  5310. struct jack_graph_port *port1_ptr,
  5311. struct jack_graph_port *port2_ptr)
  5312. {
  5313. int ret;
  5314. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  5315. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  5316. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  5317. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  5318. ret = jack_connect(controller_ptr->client, port1_name, port2_name);
  5319. if (ret != 0)
  5320. {
  5321. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  5322. return false;
  5323. }
  5324. return true;
  5325. }
  5326. static
  5327. bool
  5328. jack_controller_patchbay_disconnect(
  5329. struct jack_dbus_method_call *dbus_call_ptr,
  5330. struct jack_controller *controller_ptr,
  5331. struct jack_graph_port *port1_ptr,
  5332. struct jack_graph_port *port2_ptr)
  5333. {
  5334. int ret;
  5335. char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  5336. char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  5337. sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
  5338. sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);
  5339. ret = jack_disconnect(controller_ptr->client, port1_name, port2_name);
  5340. if (ret != 0)
  5341. {
  5342. jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
  5343. return false;
  5344. }
  5345. return true;
  5346. }
  5347. #define controller_ptr ((struct jack_controller *)call->context)
  5348. #define patchbay_ptr ((struct jack_controller_patchbay *)controller_ptr->patchbay_context)
  5349. static
  5350. void
  5351. jack_controller_dbus_get_all_ports(
  5352. struct jack_dbus_method_call * call)
  5353. {
  5354. struct list_head * client_node_ptr;
  5355. struct list_head * port_node_ptr;
  5356. struct jack_graph_client * client_ptr;
  5357. struct jack_graph_port * port_ptr;
  5358. DBusMessageIter iter, sub_iter;
  5359. char fullname[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  5360. char *fullname_var = fullname;
  5361. if (!controller_ptr->started)
  5362. {
  5363. jack_dbus_error(
  5364. call,
  5365. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  5366. "Can't execute this method with stopped JACK server");
  5367. return;
  5368. }
  5369. call->reply = dbus_message_new_method_return (call->message);
  5370. if (!call->reply)
  5371. {
  5372. goto fail;
  5373. }
  5374. dbus_message_iter_init_append (call->reply, &iter);
  5375. if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
  5376. {
  5377. goto fail_unref;
  5378. }
  5379. pthread_mutex_lock(&patchbay_ptr->lock);
  5380. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  5381. {
  5382. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  5383. list_for_each(port_node_ptr, &client_ptr->ports)
  5384. {
  5385. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  5386. jack_info("%s:%s", client_ptr->name, port_ptr->name);
  5387. sprintf(fullname, "%s:%s", client_ptr->name, port_ptr->name);
  5388. if (!dbus_message_iter_append_basic (&sub_iter, DBUS_TYPE_STRING, &fullname_var))
  5389. {
  5390. pthread_mutex_unlock(&patchbay_ptr->lock);
  5391. dbus_message_iter_close_container (&iter, &sub_iter);
  5392. goto fail_unref;
  5393. }
  5394. }
  5395. }
  5396. pthread_mutex_unlock(&patchbay_ptr->lock);
  5397. if (!dbus_message_iter_close_container (&iter, &sub_iter))
  5398. {
  5399. goto fail_unref;
  5400. }
  5401. return;
  5402. fail_unref:
  5403. dbus_message_unref (call->reply);
  5404. call->reply = NULL;
  5405. fail:
  5406. jack_error ("Ran out of memory trying to construct method return");
  5407. }
  5408. static
  5409. void
  5410. jack_controller_dbus_get_graph(
  5411. struct jack_dbus_method_call * call)
  5412. {
  5413. struct list_head * client_node_ptr;
  5414. struct list_head * port_node_ptr;
  5415. struct list_head * connection_node_ptr;
  5416. struct jack_graph_client * client_ptr;
  5417. struct jack_graph_port * port_ptr;
  5418. struct jack_graph_connection * connection_ptr;
  5419. DBusMessageIter iter;
  5420. DBusMessageIter clients_array_iter;
  5421. DBusMessageIter client_struct_iter;
  5422. DBusMessageIter ports_array_iter;
  5423. DBusMessageIter port_struct_iter;
  5424. dbus_uint64_t version;
  5425. DBusMessageIter connections_array_iter;
  5426. DBusMessageIter connection_struct_iter;
  5427. if (!controller_ptr->started)
  5428. {
  5429. jack_dbus_error(
  5430. call,
  5431. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  5432. "Can't execute this method with stopped JACK server");
  5433. return;
  5434. }
  5435. if (!jack_dbus_get_method_args(call, DBUS_TYPE_UINT64, &version, DBUS_TYPE_INVALID))
  5436. {
  5437. /* The method call had invalid arguments meaning that
  5438. * jack_dbus_get_method_args() has constructed an error for us.
  5439. */
  5440. goto exit;
  5441. }
  5442. //jack_info("Getting graph, know version is %" PRIu32, version);
  5443. call->reply = dbus_message_new_method_return(call->message);
  5444. if (!call->reply)
  5445. {
  5446. jack_error("Ran out of memory trying to construct method return");
  5447. goto exit;
  5448. }
  5449. dbus_message_iter_init_append (call->reply, &iter);
  5450. pthread_mutex_lock(&patchbay_ptr->lock);
  5451. if (version > patchbay_ptr->graph.version)
  5452. {
  5453. jack_dbus_error(
  5454. call,
  5455. JACK_DBUS_ERROR_INVALID_ARGS,
  5456. "known graph version %" PRIu64 " is newer than actual version %" PRIu64,
  5457. version,
  5458. patchbay_ptr->graph.version);
  5459. pthread_mutex_unlock(&patchbay_ptr->lock);
  5460. goto exit;
  5461. }
  5462. if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &patchbay_ptr->graph.version))
  5463. {
  5464. goto nomem_unlock;
  5465. }
  5466. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tsa(tsuu))", &clients_array_iter))
  5467. {
  5468. goto nomem_unlock;
  5469. }
  5470. if (version < patchbay_ptr->graph.version)
  5471. {
  5472. list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
  5473. {
  5474. client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);
  5475. if (!dbus_message_iter_open_container (&clients_array_iter, DBUS_TYPE_STRUCT, NULL, &client_struct_iter))
  5476. {
  5477. goto nomem_close_clients_array;
  5478. }
  5479. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_UINT64, &client_ptr->id))
  5480. {
  5481. goto nomem_close_client_struct;
  5482. }
  5483. if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_STRING, &client_ptr->name))
  5484. {
  5485. goto nomem_close_client_struct;
  5486. }
  5487. if (!dbus_message_iter_open_container(&client_struct_iter, DBUS_TYPE_ARRAY, "(tsuu)", &ports_array_iter))
  5488. {
  5489. goto nomem_close_client_struct;
  5490. }
  5491. list_for_each(port_node_ptr, &client_ptr->ports)
  5492. {
  5493. port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);
  5494. if (!dbus_message_iter_open_container(&ports_array_iter, DBUS_TYPE_STRUCT, NULL, &port_struct_iter))
  5495. {
  5496. goto nomem_close_ports_array;
  5497. }
  5498. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT64, &port_ptr->id))
  5499. {
  5500. goto nomem_close_port_struct;
  5501. }
  5502. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_STRING, &port_ptr->name))
  5503. {
  5504. goto nomem_close_port_struct;
  5505. }
  5506. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->flags))
  5507. {
  5508. goto nomem_close_port_struct;
  5509. }
  5510. if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->type))
  5511. {
  5512. goto nomem_close_port_struct;
  5513. }
  5514. if (!dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter))
  5515. {
  5516. goto nomem_close_ports_array;
  5517. }
  5518. }
  5519. if (!dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter))
  5520. {
  5521. goto nomem_close_client_struct;
  5522. }
  5523. if (!dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter))
  5524. {
  5525. goto nomem_close_clients_array;
  5526. }
  5527. }
  5528. }
  5529. if (!dbus_message_iter_close_container(&iter, &clients_array_iter))
  5530. {
  5531. goto nomem_unlock;
  5532. }
  5533. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tstststst)", &connections_array_iter))
  5534. {
  5535. goto nomem_unlock;
  5536. }
  5537. if (version < patchbay_ptr->graph.version)
  5538. {
  5539. list_for_each(connection_node_ptr, &patchbay_ptr->graph.connections)
  5540. {
  5541. connection_ptr = list_entry(connection_node_ptr, struct jack_graph_connection, siblings);
  5542. if (!dbus_message_iter_open_container(&connections_array_iter, DBUS_TYPE_STRUCT, NULL, &connection_struct_iter))
  5543. {
  5544. goto nomem_close_connections_array;
  5545. }
  5546. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->client->id))
  5547. {
  5548. goto nomem_close_connection_struct;
  5549. }
  5550. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->client->name))
  5551. {
  5552. goto nomem_close_connection_struct;
  5553. }
  5554. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port1->id))
  5555. {
  5556. goto nomem_close_connection_struct;
  5557. }
  5558. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port1->name))
  5559. {
  5560. goto nomem_close_connection_struct;
  5561. }
  5562. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->client->id))
  5563. {
  5564. goto nomem_close_connection_struct;
  5565. }
  5566. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->client->name))
  5567. {
  5568. goto nomem_close_connection_struct;
  5569. }
  5570. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->port2->id))
  5571. {
  5572. goto nomem_close_connection_struct;
  5573. }
  5574. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_STRING, &connection_ptr->port2->name))
  5575. {
  5576. goto nomem_close_connection_struct;
  5577. }
  5578. if (!dbus_message_iter_append_basic(&connection_struct_iter, DBUS_TYPE_UINT64, &connection_ptr->id))
  5579. {
  5580. goto nomem_close_connection_struct;
  5581. }
  5582. if (!dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter))
  5583. {
  5584. goto nomem_close_connections_array;
  5585. }
  5586. }
  5587. }
  5588. if (!dbus_message_iter_close_container(&iter, &connections_array_iter))
  5589. {
  5590. goto nomem_unlock;
  5591. }
  5592. pthread_mutex_unlock(&patchbay_ptr->lock);
  5593. return;
  5594. nomem_close_connection_struct:
  5595. dbus_message_iter_close_container(&connections_array_iter, &connection_struct_iter);
  5596. nomem_close_connections_array:
  5597. dbus_message_iter_close_container(&iter, &connections_array_iter);
  5598. goto nomem_unlock;
  5599. nomem_close_port_struct:
  5600. dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter);
  5601. nomem_close_ports_array:
  5602. dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter);
  5603. nomem_close_client_struct:
  5604. dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter);
  5605. nomem_close_clients_array:
  5606. dbus_message_iter_close_container(&iter, &clients_array_iter);
  5607. nomem_unlock:
  5608. pthread_mutex_unlock(&patchbay_ptr->lock);
  5609. //nomem:
  5610. dbus_message_unref(call->reply);
  5611. call->reply = NULL;
  5612. jack_error("Ran out of memory trying to construct method return");
  5613. exit:
  5614. return;
  5615. }
  5616. static
  5617. void
  5618. jack_controller_dbus_connect_ports_by_name(
  5619. struct jack_dbus_method_call * call)
  5620. {
  5621. const char * client1_name;
  5622. const char * port1_name;
  5623. const char * client2_name;
  5624. const char * port2_name;
  5625. struct jack_graph_port *port1_ptr;
  5626. struct jack_graph_port *port2_ptr;
  5627. /* jack_info("jack_controller_dbus_connect_ports_by_name() called."); */
  5628. if (!controller_ptr->started)
  5629. {
  5630. jack_dbus_error(
  5631. call,
  5632. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  5633. "Can't execute this method with stopped JACK server");
  5634. return;
  5635. }
  5636. if (!jack_dbus_get_method_args(
  5637. call,
  5638. DBUS_TYPE_STRING,
  5639. &client1_name,
  5640. DBUS_TYPE_STRING,
  5641. &port1_name,
  5642. DBUS_TYPE_STRING,
  5643. &client2_name,
  5644. DBUS_TYPE_STRING,
  5645. &port2_name,
  5646. DBUS_TYPE_INVALID))
  5647. {
  5648. /* The method call had invalid arguments meaning that
  5649. * jack_dbus_get_method_args() has constructed an error for us.
  5650. */
  5651. return;
  5652. }
  5653. /* jack_info("connecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  5654. pthread_mutex_lock(&patchbay_ptr->lock);
  5655. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  5656. if (port1_ptr == NULL)
  5657. {
  5658. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  5659. goto unlock;
  5660. }
  5661. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  5662. if (port2_ptr == NULL)
  5663. {
  5664. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  5665. goto unlock;
  5666. }
  5667. if (!jack_controller_patchbay_connect(
  5668. call,
  5669. controller_ptr,
  5670. port1_ptr,
  5671. port2_ptr))
  5672. {
  5673. /* jack_controller_patchbay_connect() constructed error reply */
  5674. goto unlock;
  5675. }
  5676. jack_dbus_construct_method_return_empty(call);
  5677. unlock:
  5678. pthread_mutex_unlock(&patchbay_ptr->lock);
  5679. }
  5680. static
  5681. void
  5682. jack_controller_dbus_connect_ports_by_id(
  5683. struct jack_dbus_method_call * call)
  5684. {
  5685. dbus_uint64_t port1_id;
  5686. dbus_uint64_t port2_id;
  5687. struct jack_graph_port *port1_ptr;
  5688. struct jack_graph_port *port2_ptr;
  5689. /* jack_info("jack_controller_dbus_connect_ports_by_id() called."); */
  5690. if (!controller_ptr->started)
  5691. {
  5692. jack_dbus_error(
  5693. call,
  5694. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  5695. "Can't execute this method with stopped JACK server");
  5696. return;
  5697. }
  5698. if (!jack_dbus_get_method_args(
  5699. call,
  5700. DBUS_TYPE_UINT64,
  5701. &port1_id,
  5702. DBUS_TYPE_UINT64,
  5703. &port2_id,
  5704. DBUS_TYPE_INVALID))
  5705. {
  5706. /* The method call had invalid arguments meaning that
  5707. * jack_dbus_get_method_args() has constructed an error for us.
  5708. */
  5709. return;
  5710. }
  5711. pthread_mutex_lock(&patchbay_ptr->lock);
  5712. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  5713. if (port1_ptr == NULL)
  5714. {
  5715. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  5716. goto unlock;
  5717. }
  5718. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  5719. if (port2_ptr == NULL)
  5720. {
  5721. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  5722. goto unlock;
  5723. }
  5724. if (!jack_controller_patchbay_connect(
  5725. call,
  5726. controller_ptr,
  5727. port1_ptr,
  5728. port2_ptr))
  5729. {
  5730. /* jack_controller_patchbay_connect() constructed error reply */
  5731. goto unlock;
  5732. }
  5733. jack_dbus_construct_method_return_empty(call);
  5734. unlock:
  5735. pthread_mutex_unlock(&patchbay_ptr->lock);
  5736. }
  5737. static
  5738. void
  5739. jack_controller_dbus_disconnect_ports_by_name(
  5740. struct jack_dbus_method_call * call)
  5741. {
  5742. const char * client1_name;
  5743. const char * port1_name;
  5744. const char * client2_name;
  5745. const char * port2_name;
  5746. struct jack_graph_port *port1_ptr;
  5747. struct jack_graph_port *port2_ptr;
  5748. /* jack_info("jack_controller_dbus_disconnect_ports_by_name() called."); */
  5749. if (!controller_ptr->started)
  5750. {
  5751. jack_dbus_error(
  5752. call,
  5753. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  5754. "Can't execute this method with stopped JACK server");
  5755. return;
  5756. }
  5757. if (!jack_dbus_get_method_args(
  5758. call,
  5759. DBUS_TYPE_STRING,
  5760. &client1_name,
  5761. DBUS_TYPE_STRING,
  5762. &port1_name,
  5763. DBUS_TYPE_STRING,
  5764. &client2_name,
  5765. DBUS_TYPE_STRING,
  5766. &port2_name,
  5767. DBUS_TYPE_INVALID))
  5768. {
  5769. /* The method call had invalid arguments meaning that
  5770. * jack_dbus_get_method_args() has constructed an error for us.
  5771. */
  5772. return;
  5773. }
  5774. /* jack_info("disconnecting %s:%s and %s:%s", client1_name, port1_name, client2_name, port2_name); */
  5775. pthread_mutex_lock(&patchbay_ptr->lock);
  5776. port1_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client1_name, port1_name);
  5777. if (port1_ptr == NULL)
  5778. {
  5779. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client1_name, port1_name);
  5780. goto unlock;
  5781. }
  5782. port2_ptr = jack_controller_patchbay_find_port_by_names(patchbay_ptr, client2_name, port2_name);
  5783. if (port2_ptr == NULL)
  5784. {
  5785. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port '%s':'%s'", client2_name, port2_name);
  5786. goto unlock;
  5787. }
  5788. if (!jack_controller_patchbay_disconnect(
  5789. call,
  5790. controller_ptr,
  5791. port1_ptr,
  5792. port2_ptr))
  5793. {
  5794. /* jack_controller_patchbay_connect() constructed error reply */
  5795. goto unlock;
  5796. }
  5797. jack_dbus_construct_method_return_empty(call);
  5798. unlock:
  5799. pthread_mutex_unlock(&patchbay_ptr->lock);
  5800. }
  5801. static
  5802. void
  5803. jack_controller_dbus_disconnect_ports_by_id(
  5804. struct jack_dbus_method_call * call)
  5805. {
  5806. dbus_uint64_t port1_id;
  5807. dbus_uint64_t port2_id;
  5808. struct jack_graph_port *port1_ptr;
  5809. struct jack_graph_port *port2_ptr;
  5810. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  5811. if (!controller_ptr->started)
  5812. {
  5813. jack_dbus_error(
  5814. call,
  5815. JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
  5816. "Can't execute this method with stopped JACK server");
  5817. return;
  5818. }
  5819. if (!jack_dbus_get_method_args(
  5820. call,
  5821. DBUS_TYPE_UINT64,
  5822. &port1_id,
  5823. DBUS_TYPE_UINT64,
  5824. &port2_id,
  5825. DBUS_TYPE_INVALID))
  5826. {
  5827. /* The method call had invalid arguments meaning that
  5828. * jack_dbus_get_method_args() has constructed an error for us.
  5829. */
  5830. return;
  5831. }
  5832. pthread_mutex_lock(&patchbay_ptr->lock);
  5833. port1_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port1_id);
  5834. if (port1_ptr == NULL)
  5835. {
  5836. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port1_id);
  5837. return;
  5838. }
  5839. port2_ptr = jack_controller_patchbay_find_port_by_id(patchbay_ptr, port2_id);
  5840. if (port2_ptr == NULL)
  5841. {
  5842. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find port %" PRIu64, port2_id);
  5843. return;
  5844. }
  5845. if (!jack_controller_patchbay_disconnect(
  5846. call,
  5847. controller_ptr,
  5848. port1_ptr,
  5849. port2_ptr))
  5850. {
  5851. /* jack_controller_patchbay_connect() constructed error reply */
  5852. goto unlock;
  5853. }
  5854. jack_dbus_construct_method_return_empty(call);
  5855. unlock:
  5856. pthread_mutex_unlock(&patchbay_ptr->lock);
  5857. }
  5858. static
  5859. void
  5860. jack_controller_dbus_disconnect_ports_by_connection_id(
  5861. struct jack_dbus_method_call * call)
  5862. {
  5863. dbus_uint64_t connection_id;
  5864. struct jack_graph_connection *connection_ptr;
  5865. /* jack_info("jack_controller_dbus_disconnect_ports_by_id() called."); */
  5866. if (!jack_dbus_get_method_args(
  5867. call,
  5868. DBUS_TYPE_UINT64,
  5869. &connection_id,
  5870. DBUS_TYPE_INVALID))
  5871. {
  5872. /* The method call had invalid arguments meaning that
  5873. * jack_dbus_get_method_args() has constructed an error for us.
  5874. */
  5875. return;
  5876. }
  5877. pthread_mutex_lock(&patchbay_ptr->lock);
  5878. connection_ptr = jack_controller_patchbay_find_connection_by_id(patchbay_ptr, connection_id);
  5879. if (connection_ptr == NULL)
  5880. {
  5881. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find connection %" PRIu64, connection_id);
  5882. goto unlock;
  5883. }
  5884. if (!jack_controller_patchbay_disconnect(
  5885. call,
  5886. controller_ptr,
  5887. connection_ptr->port1,
  5888. connection_ptr->port2))
  5889. {
  5890. /* jack_controller_patchbay_connect() constructed error reply */
  5891. goto unlock;
  5892. }
  5893. jack_dbus_construct_method_return_empty(call);
  5894. unlock:
  5895. pthread_mutex_unlock(&patchbay_ptr->lock);
  5896. }
  5897. static
  5898. void
  5899. jack_controller_dbus_get_client_pid(
  5900. struct jack_dbus_method_call * call)
  5901. {
  5902. dbus_uint64_t client_id;
  5903. struct jack_graph_client *client_ptr;
  5904. message_arg_t arg;
  5905. /* jack_info("jack_controller_dbus_get_client_pid() called."); */
  5906. if (!jack_dbus_get_method_args(
  5907. call,
  5908. DBUS_TYPE_UINT64,
  5909. &client_id,
  5910. DBUS_TYPE_INVALID))
  5911. {
  5912. /* The method call had invalid arguments meaning that
  5913. * jack_dbus_get_method_args() has constructed an error for us.
  5914. */
  5915. return;
  5916. }
  5917. pthread_mutex_lock(&patchbay_ptr->lock);
  5918. client_ptr = jack_controller_patchbay_find_client_by_id(patchbay_ptr, client_id);
  5919. if (client_ptr == NULL)
  5920. {
  5921. jack_dbus_error(call, JACK_DBUS_ERROR_INVALID_ARGS, "cannot find client %" PRIu64, client_id);
  5922. goto unlock;
  5923. }
  5924. arg.int64 = client_ptr->pid;
  5925. jack_dbus_construct_method_return_single(call, DBUS_TYPE_INT64, arg);
  5926. unlock:
  5927. pthread_mutex_unlock(&patchbay_ptr->lock);
  5928. }
  5929. #undef controller_ptr
  5930. #define controller_ptr ((struct jack_controller *)context)
  5931. static
  5932. int
  5933. jack_controller_graph_order_callback(
  5934. void *context)
  5935. {
  5936. const char **ports;
  5937. int i;
  5938. jack_port_t *port_ptr;
  5939. if (patchbay_ptr->graph.version > 1)
  5940. {
  5941. /* we use this only for initial catchup */
  5942. return 0;
  5943. }
  5944. ports = jack_get_ports(controller_ptr->client, NULL, NULL, 0);
  5945. if (ports)
  5946. {
  5947. for (i = 0; ports[i]; ++i)
  5948. {
  5949. jack_info("graph reorder: new port '%s'", ports[i]);
  5950. port_ptr = jack_port_by_name(controller_ptr->client, ports[i]);;
  5951. jack_controller_patchbay_new_port(patchbay_ptr, ports[i], jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  5952. }
  5953. free(ports);
  5954. }
  5955. if (patchbay_ptr->graph.version == 1)
  5956. {
  5957. /* we have empty initial graph, increment graph version,
  5958. so we dont do jack_get_ports() again,
  5959. on next next graph change */
  5960. patchbay_ptr->graph.version++;
  5961. }
  5962. return 0;
  5963. }
  5964. void
  5965. jack_controller_client_registration_callback(
  5966. const char *client_name,
  5967. int created,
  5968. void *context)
  5969. {
  5970. if (created)
  5971. {
  5972. jack_log("client '%s' created", client_name);
  5973. jack_controller_patchbay_create_client(patchbay_ptr, client_name, strlen(client_name));
  5974. }
  5975. else
  5976. {
  5977. jack_log("client '%s' destroyed", client_name);
  5978. jack_controller_patchbay_destroy_client_by_name(patchbay_ptr, client_name);
  5979. }
  5980. }
  5981. void
  5982. jack_controller_port_registration_callback(
  5983. jack_port_id_t port_id,
  5984. int created,
  5985. void *context)
  5986. {
  5987. jack_port_t *port_ptr;
  5988. struct jack_graph_port *graph_port_ptr;
  5989. const char *port_name;
  5990. port_ptr = jack_port_by_id(controller_ptr->client, port_id);
  5991. port_name = jack_port_name(port_ptr);
  5992. if (created)
  5993. {
  5994. jack_log("port '%s' created", port_name);
  5995. jack_controller_patchbay_new_port(patchbay_ptr, port_name, jack_port_flags(port_ptr), jack_port_type_id(port_ptr));
  5996. }
  5997. else
  5998. {
  5999. jack_log("port '%s' destroyed", port_name);
  6000. graph_port_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port_name);
  6001. if (graph_port_ptr == NULL)
  6002. {
  6003. jack_error("Failed to find port '%s' to destroy", port_name);
  6004. return;
  6005. }
  6006. jack_controller_patchbay_remove_port(patchbay_ptr, graph_port_ptr);
  6007. }
  6008. }
  6009. void
  6010. jack_controller_port_connect_callback(
  6011. jack_port_id_t port1_id,
  6012. jack_port_id_t port2_id,
  6013. int connect,
  6014. void *context)
  6015. {
  6016. jack_port_t *port1;
  6017. jack_port_t *port2;
  6018. const char *port1_name;
  6019. const char *port2_name;
  6020. struct jack_graph_port *port1_ptr;
  6021. struct jack_graph_port *port2_ptr;
  6022. struct jack_graph_connection *connection_ptr;
  6023. port1 = jack_port_by_id(controller_ptr->client, port1_id);
  6024. port2 = jack_port_by_id(controller_ptr->client, port2_id);
  6025. port1_name = jack_port_name(port1);
  6026. port2_name = jack_port_name(port2);
  6027. port1_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port1_name);
  6028. if (port1_ptr == NULL)
  6029. {
  6030. jack_error("Failed to find port '%s' to [dis]connect", port1_name);
  6031. return;
  6032. }
  6033. port2_ptr = jack_controller_patchbay_find_port_by_full_name(patchbay_ptr, port2_name);
  6034. if (port2_ptr == NULL)
  6035. {
  6036. jack_error("Failed to find port '%s' to [dis]connect", port2_name);
  6037. return;
  6038. }
  6039. if (connect)
  6040. {
  6041. jack_info("Connecting '%s' to '%s'", port1_name, port2_name);
  6042. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  6043. if (connection_ptr != NULL)
  6044. {
  6045. jack_error("'%s' and '%s' are already connected", port1_name, port2_name);
  6046. return;
  6047. }
  6048. jack_controller_patchbay_create_connection(patchbay_ptr, port1_ptr, port2_ptr);
  6049. }
  6050. else
  6051. {
  6052. jack_info("Disonnecting '%s' from '%s'", port1_name, port2_name);
  6053. connection_ptr = jack_controller_patchbay_find_connection(patchbay_ptr, port1_ptr, port2_ptr);
  6054. if (connection_ptr == NULL)
  6055. {
  6056. jack_error("Cannot find connection being removed");
  6057. return;
  6058. }
  6059. jack_controller_patchbay_destroy_connection(patchbay_ptr, connection_ptr);
  6060. }
  6061. }
  6062. #undef controller_ptr
  6063. void
  6064. jack_controller_patchbay_uninit(
  6065. struct jack_controller * controller_ptr)
  6066. {
  6067. struct jack_graph_client *client_ptr;
  6068. struct jack_graph_port *port_ptr;
  6069. /* jack_info("jack_controller_patchbay_uninit() called"); */
  6070. while (!list_empty(&patchbay_ptr->graph.ports))
  6071. {
  6072. port_ptr = list_entry(patchbay_ptr->graph.ports.next, struct jack_graph_port, siblings_graph);
  6073. jack_controller_patchbay_remove_port(patchbay_ptr, port_ptr);
  6074. }
  6075. while (!list_empty(&patchbay_ptr->graph.clients))
  6076. {
  6077. client_ptr = list_entry(patchbay_ptr->graph.clients.next, struct jack_graph_client, siblings);
  6078. jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
  6079. }
  6080. pthread_mutex_destroy(&patchbay_ptr->lock);
  6081. }
  6082. #undef patchbay_ptr
  6083. bool
  6084. jack_controller_patchbay_init(
  6085. struct jack_controller * controller_ptr)
  6086. {
  6087. int ret;
  6088. struct jack_controller_patchbay * patchbay_ptr;
  6089. pthread_mutexattr_t attr;
  6090. /* jack_info("jack_controller_patchbay_init() called"); */
  6091. patchbay_ptr = malloc(sizeof(struct jack_controller_patchbay));
  6092. if (patchbay_ptr == NULL)
  6093. {
  6094. jack_error("Memory allocation of jack_controller_patchbay structure failed.");
  6095. goto fail;
  6096. }
  6097. ret = pthread_mutexattr_init(&attr);
  6098. if (ret != 0)
  6099. {
  6100. goto fail;
  6101. }
  6102. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  6103. if (ret != 0)
  6104. {
  6105. goto fail;
  6106. }
  6107. pthread_mutex_init(&patchbay_ptr->lock, &attr);
  6108. INIT_LIST_HEAD(&patchbay_ptr->graph.clients);
  6109. INIT_LIST_HEAD(&patchbay_ptr->graph.ports);
  6110. INIT_LIST_HEAD(&patchbay_ptr->graph.connections);
  6111. patchbay_ptr->graph.version = 1;
  6112. patchbay_ptr->next_client_id = 1;
  6113. patchbay_ptr->next_port_id = 1;
  6114. patchbay_ptr->next_connection_id = 1;
  6115. controller_ptr->patchbay_context = patchbay_ptr;
  6116. ret = jack_set_graph_order_callback(controller_ptr->client, jack_controller_graph_order_callback, controller_ptr);
  6117. if (ret != 0)
  6118. {
  6119. jack_error("jack_set_graph_order_callback() failed with error %d", ret);
  6120. goto fail_uninit_mutex;
  6121. }
  6122. ret = jack_set_client_registration_callback(controller_ptr->client, jack_controller_client_registration_callback, controller_ptr);
  6123. if (ret != 0)
  6124. {
  6125. jack_error("jack_set_client_registration_callback() failed with error %d", ret);
  6126. goto fail_uninit_mutex;
  6127. }
  6128. ret = jack_set_port_registration_callback(controller_ptr->client, jack_controller_port_registration_callback, controller_ptr);
  6129. if (ret != 0)
  6130. {
  6131. jack_error("jack_set_port_registration_callback() failed with error %d", ret);
  6132. goto fail_uninit_mutex;
  6133. }
  6134. ret = jack_set_port_connect_callback(controller_ptr->client, jack_controller_port_connect_callback, controller_ptr);
  6135. if (ret != 0)
  6136. {
  6137. jack_error("jack_set_port_connect_callback() failed with error %d", ret);
  6138. goto fail_uninit_mutex;
  6139. }
  6140. return true;
  6141. fail_uninit_mutex:
  6142. pthread_mutex_destroy(&patchbay_ptr->lock);
  6143. fail:
  6144. return false;
  6145. }
  6146. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetAllPorts)
  6147. JACK_DBUS_METHOD_ARGUMENT("ports_list", "as", true)
  6148. JACK_DBUS_METHOD_ARGUMENTS_END
  6149. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetGraph)
  6150. JACK_DBUS_METHOD_ARGUMENT("known_graph_version", DBUS_TYPE_UINT64_AS_STRING, false)
  6151. JACK_DBUS_METHOD_ARGUMENT("current_graph_version", DBUS_TYPE_UINT64_AS_STRING, true)
  6152. JACK_DBUS_METHOD_ARGUMENT("clients_and_ports", "a(tsa(tsuu))", true)
  6153. JACK_DBUS_METHOD_ARGUMENT("connections", "a(tstststst)", true)
  6154. JACK_DBUS_METHOD_ARGUMENTS_END
  6155. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByName)
  6156. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  6157. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  6158. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  6159. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  6160. JACK_DBUS_METHOD_ARGUMENTS_END
  6161. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(ConnectPortsByID)
  6162. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  6163. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  6164. JACK_DBUS_METHOD_ARGUMENTS_END
  6165. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByName)
  6166. JACK_DBUS_METHOD_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING, false)
  6167. JACK_DBUS_METHOD_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING, false)
  6168. JACK_DBUS_METHOD_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING, false)
  6169. JACK_DBUS_METHOD_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING, false)
  6170. JACK_DBUS_METHOD_ARGUMENTS_END
  6171. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByID)
  6172. JACK_DBUS_METHOD_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING, false)
  6173. JACK_DBUS_METHOD_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING, false)
  6174. JACK_DBUS_METHOD_ARGUMENTS_END
  6175. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(DisconnectPortsByConnectionID)
  6176. JACK_DBUS_METHOD_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING, false)
  6177. JACK_DBUS_METHOD_ARGUMENTS_END
  6178. JACK_DBUS_METHOD_ARGUMENTS_BEGIN(GetClientPID)
  6179. JACK_DBUS_METHOD_ARGUMENT("client_id", DBUS_TYPE_INT64_AS_STRING, false)
  6180. JACK_DBUS_METHOD_ARGUMENTS_END
  6181. JACK_DBUS_METHODS_BEGIN
  6182. JACK_DBUS_METHOD_DESCRIBE(GetAllPorts, jack_controller_dbus_get_all_ports)
  6183. JACK_DBUS_METHOD_DESCRIBE(GetGraph, jack_controller_dbus_get_graph)
  6184. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByName, jack_controller_dbus_connect_ports_by_name)
  6185. JACK_DBUS_METHOD_DESCRIBE(ConnectPortsByID, jack_controller_dbus_connect_ports_by_id)
  6186. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByName, jack_controller_dbus_disconnect_ports_by_name)
  6187. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByID, jack_controller_dbus_disconnect_ports_by_id)
  6188. JACK_DBUS_METHOD_DESCRIBE(DisconnectPortsByConnectionID, jack_controller_dbus_disconnect_ports_by_connection_id)
  6189. JACK_DBUS_METHOD_DESCRIBE(GetClientPID, jack_controller_dbus_get_client_pid)
  6190. JACK_DBUS_METHODS_END
  6191. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(GraphChanged)
  6192. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  6193. JACK_DBUS_SIGNAL_ARGUMENTS_END
  6194. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientAppeared)
  6195. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  6196. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  6197. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  6198. JACK_DBUS_SIGNAL_ARGUMENTS_END
  6199. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(ClientDisappeared)
  6200. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  6201. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  6202. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  6203. JACK_DBUS_SIGNAL_ARGUMENTS_END
  6204. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortAppeared)
  6205. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  6206. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  6207. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  6208. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  6209. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  6210. JACK_DBUS_SIGNAL_ARGUMENT("port_flags", DBUS_TYPE_UINT32_AS_STRING)
  6211. JACK_DBUS_SIGNAL_ARGUMENT("port_type", DBUS_TYPE_UINT32_AS_STRING)
  6212. JACK_DBUS_SIGNAL_ARGUMENTS_END
  6213. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortDisappeared)
  6214. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  6215. JACK_DBUS_SIGNAL_ARGUMENT("client_id", DBUS_TYPE_UINT64_AS_STRING)
  6216. JACK_DBUS_SIGNAL_ARGUMENT("client_name", DBUS_TYPE_STRING_AS_STRING)
  6217. JACK_DBUS_SIGNAL_ARGUMENT("port_id", DBUS_TYPE_UINT64_AS_STRING)
  6218. JACK_DBUS_SIGNAL_ARGUMENT("port_name", DBUS_TYPE_STRING_AS_STRING)
  6219. JACK_DBUS_SIGNAL_ARGUMENTS_END
  6220. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsConnected)
  6221. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  6222. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  6223. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  6224. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  6225. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  6226. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  6227. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  6228. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  6229. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  6230. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  6231. JACK_DBUS_SIGNAL_ARGUMENTS_END
  6232. JACK_DBUS_SIGNAL_ARGUMENTS_BEGIN(PortsDisconnected)
  6233. JACK_DBUS_SIGNAL_ARGUMENT("new_graph_version", DBUS_TYPE_UINT64_AS_STRING)
  6234. JACK_DBUS_SIGNAL_ARGUMENT("client1_id", DBUS_TYPE_UINT64_AS_STRING)
  6235. JACK_DBUS_SIGNAL_ARGUMENT("client1_name", DBUS_TYPE_STRING_AS_STRING)
  6236. JACK_DBUS_SIGNAL_ARGUMENT("port1_id", DBUS_TYPE_UINT64_AS_STRING)
  6237. JACK_DBUS_SIGNAL_ARGUMENT("port1_name", DBUS_TYPE_STRING_AS_STRING)
  6238. JACK_DBUS_SIGNAL_ARGUMENT("client2_id", DBUS_TYPE_UINT64_AS_STRING)
  6239. JACK_DBUS_SIGNAL_ARGUMENT("client2_name", DBUS_TYPE_STRING_AS_STRING)
  6240. JACK_DBUS_SIGNAL_ARGUMENT("port2_id", DBUS_TYPE_UINT64_AS_STRING)
  6241. JACK_DBUS_SIGNAL_ARGUMENT("port2_name", DBUS_TYPE_STRING_AS_STRING)
  6242. JACK_DBUS_SIGNAL_ARGUMENT("connection_id", DBUS_TYPE_UINT64_AS_STRING)
  6243. JACK_DBUS_SIGNAL_ARGUMENTS_END
  6244. JACK_DBUS_SIGNALS_BEGIN
  6245. JACK_DBUS_SIGNAL_DESCRIBE(GraphChanged)
  6246. JACK_DBUS_SIGNAL_DESCRIBE(ClientAppeared)
  6247. JACK_DBUS_SIGNAL_DESCRIBE(ClientDisappeared)
  6248. JACK_DBUS_SIGNAL_DESCRIBE(PortAppeared)
  6249. JACK_DBUS_SIGNAL_DESCRIBE(PortDisappeared)
  6250. JACK_DBUS_SIGNAL_DESCRIBE(PortsConnected)
  6251. JACK_DBUS_SIGNAL_DESCRIBE(PortsDisconnected)
  6252. JACK_DBUS_SIGNALS_END
  6253. JACK_DBUS_IFACE_BEGIN(g_jack_controller_iface_patchbay, JACK_DBUS_IFACE_NAME)
  6254. JACK_DBUS_IFACE_EXPOSE_METHODS
  6255. JACK_DBUS_IFACE_EXPOSE_SIGNALS
  6256. JACK_DBUS_IFACE_END