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.

178 lines
4.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. int connecting, disconnecting;
  56. int port1_flags, port2_flags;
  57. int rc = 1;
  58. struct option long_options[] = {
  59. { "server", 1, 0, 's' },
  60. { "help", 0, 0, 'h' },
  61. { "version", 0, 0, 'v' },
  62. { 0, 0, 0, 0 }
  63. };
  64. while ((c = getopt_long (argc, argv, "s:hv", long_options, &option_index)) >= 0) {
  65. switch (c) {
  66. case 's':
  67. server_name = (char *) malloc (sizeof (char) * strlen(optarg));
  68. strcpy (server_name, optarg);
  69. options |= JackServerName;
  70. break;
  71. case 'h':
  72. show_usage (my_name);
  73. return 1;
  74. break;
  75. case 'v':
  76. show_version (my_name);
  77. return 1;
  78. break;
  79. default:
  80. show_usage (my_name);
  81. return 1;
  82. break;
  83. }
  84. }
  85. connecting = disconnecting = FALSE;
  86. if (my_name == 0) {
  87. my_name = argv[0];
  88. } else {
  89. my_name ++;
  90. }
  91. if (strstr(my_name, "disconnect")) {
  92. disconnecting = 1;
  93. } else if (strstr(my_name, "connect")) {
  94. connecting = 1;
  95. } else {
  96. fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
  97. return 1;
  98. }
  99. if (argc < 3) show_usage(my_name);
  100. /* try to become a client of the JACK server */
  101. if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
  102. fprintf (stderr, "jack server not running?\n");
  103. return 1;
  104. }
  105. /* find the two ports */
  106. if ((port1 = jack_port_by_name(client, argv[argc-1])) == 0) {
  107. fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-1]);
  108. goto exit;
  109. }
  110. if ((port2 = jack_port_by_name(client, argv[argc-2])) == 0) {
  111. fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-2]);
  112. goto exit;
  113. }
  114. port1_flags = jack_port_flags (port1);
  115. port2_flags = jack_port_flags (port2);
  116. if (port1_flags & JackPortIsInput) {
  117. if (port2_flags & JackPortIsOutput) {
  118. src_port = port2;
  119. dst_port = port1;
  120. }
  121. } else {
  122. if (port2_flags & JackPortIsInput) {
  123. src_port = port1;
  124. dst_port = port2;
  125. }
  126. }
  127. if (!src_port || !dst_port) {
  128. fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
  129. goto exit;
  130. }
  131. /* connect the ports. Note: you can't do this before
  132. the client is activated (this may change in the future).
  133. */
  134. if (connecting) {
  135. if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  136. goto exit;
  137. }
  138. }
  139. if (disconnecting) {
  140. if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  141. goto exit;
  142. }
  143. }
  144. /* everything was ok, so setting exitcode to 0 */
  145. rc = 0;
  146. exit:
  147. jack_client_close (client);
  148. exit (rc);
  149. }