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.

173 lines
4.2KB

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