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.

251 lines
6.2KB

  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. #ifndef WIN32
  18. #include <unistd.h>
  19. #endif
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <getopt.h>
  24. #include <jack/jack.h>
  25. #include <jack/session.h>
  26. jack_port_t *input_port;
  27. jack_port_t *output_port;
  28. int connecting, disconnecting;
  29. volatile int done = 0;
  30. #define TRUE 1
  31. #define FALSE 0
  32. void port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  33. {
  34. done = 1;
  35. }
  36. void
  37. show_version (char *my_name)
  38. {
  39. //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
  40. }
  41. void
  42. show_usage (char *my_name)
  43. {
  44. show_version (my_name);
  45. fprintf (stderr, "\nusage: %s [options] port1 port2\n", my_name);
  46. fprintf (stderr, "Connects two JACK ports together.\n\n");
  47. fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
  48. fprintf (stderr, " -v, --version Output version information and exit\n");
  49. fprintf (stderr, " -h, --help Display this help message\n\n");
  50. fprintf (stderr, "For more information see http://jackaudio.org/\n");
  51. }
  52. int
  53. main (int argc, char *argv[])
  54. {
  55. jack_client_t *client;
  56. jack_status_t status;
  57. char *server_name = NULL;
  58. int c;
  59. int option_index;
  60. jack_options_t options = JackNoStartServer;
  61. char *my_name = strrchr(argv[0], '/');
  62. jack_port_t *src_port = 0;
  63. jack_port_t *dst_port = 0;
  64. jack_port_t *port1 = 0;
  65. jack_port_t *port2 = 0;
  66. char portA[300];
  67. char portB[300];
  68. int use_uuid=0;
  69. int connecting, disconnecting;
  70. int port1_flags, port2_flags;
  71. int rc = 1;
  72. struct option long_options[] = {
  73. { "server", 1, 0, 's' },
  74. { "help", 0, 0, 'h' },
  75. { "version", 0, 0, 'v' },
  76. { "uuid", 0, 0, 'u' },
  77. { 0, 0, 0, 0 }
  78. };
  79. while ((c = getopt_long (argc, argv, "s:hvu", long_options, &option_index)) >= 0) {
  80. switch (c) {
  81. case 's':
  82. server_name = (char *) malloc (sizeof (char) * strlen(optarg));
  83. strcpy (server_name, optarg);
  84. options |= JackServerName;
  85. break;
  86. case 'u':
  87. use_uuid = 1;
  88. break;
  89. case 'h':
  90. show_usage (my_name);
  91. return 1;
  92. break;
  93. case 'v':
  94. show_version (my_name);
  95. return 1;
  96. break;
  97. default:
  98. show_usage (my_name);
  99. return 1;
  100. break;
  101. }
  102. }
  103. connecting = disconnecting = FALSE;
  104. if (my_name == 0) {
  105. my_name = argv[0];
  106. } else {
  107. my_name ++;
  108. }
  109. if (strstr(my_name, "disconnect")) {
  110. disconnecting = 1;
  111. } else if (strstr(my_name, "connect")) {
  112. connecting = 1;
  113. } else {
  114. fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
  115. return 1;
  116. }
  117. if (argc < 3) {
  118. show_usage(my_name);
  119. return 1;
  120. }
  121. /* try to become a client of the JACK server */
  122. if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
  123. fprintf (stderr, "JACK server not running?\n");
  124. return 1;
  125. }
  126. jack_set_port_connect_callback(client, port_connect_callback, NULL);
  127. /* find the two ports */
  128. if( use_uuid ) {
  129. char *tmpname;
  130. char *clientname;
  131. char *portname;
  132. tmpname = strdup( argv[argc-1] );
  133. portname = strchr( tmpname, ':' );
  134. portname[0] = '\0';
  135. portname+=1;
  136. clientname = jack_get_client_name_by_uuid( client, tmpname );
  137. if( clientname ) {
  138. snprintf( portA, sizeof(portA), "%s:%s", clientname, portname );
  139. jack_free( clientname );
  140. } else {
  141. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  142. }
  143. free( tmpname );
  144. tmpname = strdup( argv[argc-2] );
  145. portname = strchr( tmpname, ':' );
  146. portname[0] = '\0';
  147. portname+=1;
  148. clientname = jack_get_client_name_by_uuid( client, tmpname );
  149. if( clientname ) {
  150. snprintf( portB, sizeof(portB), "%s:%s", clientname, portname );
  151. jack_free( clientname );
  152. } else {
  153. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  154. }
  155. free( tmpname );
  156. } else {
  157. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  158. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  159. }
  160. if ((port1 = jack_port_by_name(client, portA)) == 0) {
  161. fprintf (stderr, "ERROR %s not a valid port\n", portA);
  162. goto exit;
  163. }
  164. if ((port2 = jack_port_by_name(client, portB)) == 0) {
  165. fprintf (stderr, "ERROR %s not a valid port\n", portB);
  166. goto exit;
  167. }
  168. port1_flags = jack_port_flags (port1);
  169. port2_flags = jack_port_flags (port2);
  170. if (port1_flags & JackPortIsInput) {
  171. if (port2_flags & JackPortIsOutput) {
  172. src_port = port2;
  173. dst_port = port1;
  174. }
  175. } else {
  176. if (port2_flags & JackPortIsInput) {
  177. src_port = port1;
  178. dst_port = port2;
  179. }
  180. }
  181. if (!src_port || !dst_port) {
  182. fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
  183. goto exit;
  184. }
  185. /* tell the JACK server that we are ready to roll */
  186. if (jack_activate (client)) {
  187. fprintf (stderr, "cannot activate client");
  188. goto exit;
  189. }
  190. /* connect the ports. Note: you can't do this before
  191. the client is activated (this may change in the future).
  192. */
  193. if (connecting) {
  194. if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  195. fprintf (stderr, "cannot connect client, already connected?\n");
  196. goto exit;
  197. }
  198. }
  199. if (disconnecting) {
  200. if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  201. fprintf (stderr, "cannot disconnect client, already disconnected?\n");
  202. goto exit;
  203. }
  204. }
  205. // Wait for connection/disconnection to be effective
  206. while(!done) {
  207. #ifdef WIN32
  208. Sleep(10);
  209. #else
  210. usleep(10000);
  211. #endif
  212. }
  213. /* everything was ok, so setting exitcode to 0 */
  214. rc = 0;
  215. exit:
  216. jack_client_close (client);
  217. exit (rc);
  218. }