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.

241 lines
5.8KB

  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. 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) show_usage(my_name);
  118. /* try to become a client of the JACK server */
  119. if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
  120. fprintf (stderr, "jack server not running?\n");
  121. return 1;
  122. }
  123. jack_set_port_connect_callback(client, port_connect_callback, NULL);
  124. /* find the two ports */
  125. if( use_uuid ) {
  126. char *tmpname;
  127. char *clientname;
  128. char *portname;
  129. tmpname = strdup( argv[argc-1] );
  130. portname = strchr( tmpname, ':' );
  131. portname[0] = '\0';
  132. portname+=1;
  133. clientname = jack_get_client_name_by_uuid( client, tmpname );
  134. if( clientname ) {
  135. snprintf( portA, sizeof(portA), "%s:%s", clientname, portname );
  136. jack_free( clientname );
  137. } else {
  138. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  139. }
  140. free( tmpname );
  141. tmpname = strdup( argv[argc-2] );
  142. portname = strchr( tmpname, ':' );
  143. portname[0] = '\0';
  144. portname+=1;
  145. clientname = jack_get_client_name_by_uuid( client, tmpname );
  146. if( clientname ) {
  147. snprintf( portB, sizeof(portB), "%s:%s", clientname, portname );
  148. jack_free( clientname );
  149. } else {
  150. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  151. }
  152. free( tmpname );
  153. } else {
  154. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  155. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  156. }
  157. if ((port1 = jack_port_by_name(client, portA)) == 0) {
  158. fprintf (stderr, "ERROR %s not a valid port\n", portA);
  159. goto exit;
  160. }
  161. if ((port2 = jack_port_by_name(client, portB)) == 0) {
  162. fprintf (stderr, "ERROR %s not a valid port\n", portB);
  163. goto exit;
  164. }
  165. port1_flags = jack_port_flags (port1);
  166. port2_flags = jack_port_flags (port2);
  167. if (port1_flags & JackPortIsInput) {
  168. if (port2_flags & JackPortIsOutput) {
  169. src_port = port2;
  170. dst_port = port1;
  171. }
  172. } else {
  173. if (port2_flags & JackPortIsInput) {
  174. src_port = port1;
  175. dst_port = port2;
  176. }
  177. }
  178. if (!src_port || !dst_port) {
  179. fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
  180. goto exit;
  181. }
  182. /* connect the ports. Note: you can't do this before
  183. the client is activated (this may change in the future).
  184. */
  185. if (connecting) {
  186. if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  187. goto exit;
  188. }
  189. }
  190. if (disconnecting) {
  191. if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  192. goto exit;
  193. }
  194. }
  195. // Wait for connection/disconnection to be effective
  196. while(!done) {
  197. #ifdef WIN32
  198. Sleep(10);
  199. #else
  200. usleep(10000);
  201. #endif
  202. }
  203. /* everything was ok, so setting exitcode to 0 */
  204. rc = 0;
  205. exit:
  206. jack_client_close (client);
  207. exit (rc);
  208. }