jack1 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.

220 lines
5.3KB

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