JACK tools
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.

226 lines
5.4KB

  1. /*
  2. Copyright (C) 2002 Jeremy Hall
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <getopt.h>
  21. #include <config.h>
  22. #include <jack/jack.h>
  23. #include <jack/session.h>
  24. #define TRUE 1
  25. #define FALSE 0
  26. void
  27. show_version (char *my_name)
  28. {
  29. fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
  30. }
  31. void
  32. show_usage (char *my_name)
  33. {
  34. show_version (my_name);
  35. fprintf (stderr, "\nusage: %s [options] port1 port2\n", my_name);
  36. fprintf (stderr, "Connects two JACK ports together.\n\n");
  37. fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
  38. fprintf (stderr, " -v, --version Output version information and exit\n");
  39. fprintf (stderr, " -h, --help Display this help message\n\n");
  40. fprintf (stderr, "For more information see http://jackaudio.org/\n");
  41. }
  42. int
  43. main (int argc, char *argv[])
  44. {
  45. jack_client_t *client;
  46. jack_status_t status;
  47. char *server_name = NULL;
  48. int c;
  49. int option_index;
  50. jack_options_t options = JackNoStartServer;
  51. char *my_name = strrchr(argv[0], '/');
  52. jack_port_t *src_port = 0;
  53. jack_port_t *dst_port = 0;
  54. jack_port_t *port1 = 0;
  55. jack_port_t *port2 = 0;
  56. char portA[300];
  57. char portB[300];
  58. int use_uuid=0;
  59. int connecting, disconnecting;
  60. int port1_flags, port2_flags;
  61. int rc = 1;
  62. struct option long_options[] = {
  63. { "server", 1, 0, 's' },
  64. { "help", 0, 0, 'h' },
  65. { "version", 0, 0, 'v' },
  66. { "uuid", 0, 0, 'u' },
  67. { 0, 0, 0, 0 }
  68. };
  69. while ((c = getopt_long (argc, argv, "s:hvu", long_options, &option_index)) >= 0) {
  70. switch (c) {
  71. case 's':
  72. server_name = (char *) malloc (sizeof (char) * strlen(optarg));
  73. strcpy (server_name, optarg);
  74. options |= JackServerName;
  75. break;
  76. case 'u':
  77. use_uuid = 1;
  78. break;
  79. case 'h':
  80. show_usage (my_name);
  81. return 1;
  82. break;
  83. case 'v':
  84. show_version (my_name);
  85. return 1;
  86. break;
  87. default:
  88. show_usage (my_name);
  89. return 1;
  90. break;
  91. }
  92. }
  93. connecting = disconnecting = FALSE;
  94. if (my_name == 0) {
  95. my_name = argv[0];
  96. } else {
  97. my_name ++;
  98. }
  99. if (strstr(my_name, "disconnect")) {
  100. disconnecting = 1;
  101. } else if (strstr(my_name, "connect")) {
  102. connecting = 1;
  103. } else {
  104. fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
  105. return 1;
  106. }
  107. if (argc < 3) {
  108. show_usage(my_name);
  109. return 1;
  110. }
  111. /* try to become a client of the JACK server */
  112. if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
  113. fprintf (stderr, "jack server not running?\n");
  114. return 1;
  115. }
  116. /* find the two ports */
  117. if( use_uuid ) {
  118. char *tmpname;
  119. char *clientname;
  120. char *portname;
  121. tmpname = strdup( argv[argc-1] );
  122. portname = strchr( tmpname, ':' );
  123. portname[0] = '\0';
  124. portname+=1;
  125. clientname = jack_get_client_name_by_uuid( client, tmpname );
  126. if( clientname ) {
  127. snprintf( portA, sizeof(portA), "%s:%s", clientname, portname );
  128. jack_free( clientname );
  129. } else {
  130. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  131. }
  132. free( tmpname );
  133. tmpname = strdup( argv[argc-2] );
  134. portname = strchr( tmpname, ':' );
  135. portname[0] = '\0';
  136. portname+=1;
  137. clientname = jack_get_client_name_by_uuid( client, tmpname );
  138. if( clientname ) {
  139. snprintf( portB, sizeof(portB), "%s:%s", clientname, portname );
  140. jack_free( clientname );
  141. } else {
  142. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  143. }
  144. free( tmpname );
  145. } else {
  146. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  147. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  148. }
  149. if ((port1 = jack_port_by_name(client, portA)) == 0) {
  150. fprintf (stderr, "ERROR %s not a valid port\n", portA);
  151. goto exit;
  152. }
  153. if ((port2 = jack_port_by_name(client, portB)) == 0) {
  154. fprintf (stderr, "ERROR %s not a valid port\n", portB);
  155. goto exit;
  156. }
  157. port1_flags = jack_port_flags (port1);
  158. port2_flags = jack_port_flags (port2);
  159. if (port1_flags & JackPortIsInput) {
  160. if (port2_flags & JackPortIsOutput) {
  161. src_port = port2;
  162. dst_port = port1;
  163. }
  164. } else {
  165. if (port2_flags & JackPortIsInput) {
  166. src_port = port1;
  167. dst_port = port2;
  168. }
  169. }
  170. if (!src_port || !dst_port) {
  171. fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
  172. goto exit;
  173. }
  174. /* connect the ports. Note: you can't do this before
  175. the client is activated (this may change in the future).
  176. */
  177. if (connecting) {
  178. if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  179. goto exit;
  180. }
  181. }
  182. if (disconnecting) {
  183. if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  184. goto exit;
  185. }
  186. }
  187. /* everything was ok, so setting exitcode to 0 */
  188. rc = 0;
  189. exit:
  190. jack_client_close (client);
  191. exit (rc);
  192. }