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.

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