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.

249 lines
6.0KB

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