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.

297 lines
7.7KB

  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 <getopt.h>
  23. #include <jack/jack.h>
  24. #include <jack/metadata.h>
  25. #include <jack/session.h>
  26. #include <jack/types.h>
  27. #define TRUE 1
  28. #define FALSE 0
  29. volatile int done = 0;
  30. void port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  31. {
  32. done = 1;
  33. }
  34. void
  35. show_version (char *my_name)
  36. {
  37. //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
  38. }
  39. void
  40. show_usage (char *my_name)
  41. {
  42. show_version (my_name);
  43. fprintf (stderr, "\nusage: %s [options] port1 port2\n", my_name);
  44. fprintf (stderr, "Connects two JACK ports together.\n\n");
  45. fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
  46. fprintf (stderr, " -u, --uuid Reference ports by their UUID instead of their name\n");
  47. fprintf (stderr, " -f, --force Force connect even if the ports may not be compatible\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 force_connect=0;
  70. int connecting, disconnecting;
  71. int port1_flags, port2_flags;
  72. int rc = 1;
  73. struct option long_options[] = {
  74. { "server", 1, 0, 's' },
  75. { "help", 0, 0, 'h' },
  76. { "version", 0, 0, 'v' },
  77. { "uuid", 0, 0, 'u' },
  78. { "force", 0, 0, 'f' },
  79. { 0, 0, 0, 0 }
  80. };
  81. while ((c = getopt_long (argc, argv, "s:fhvu", long_options, &option_index)) >= 0) {
  82. switch (c) {
  83. case 's':
  84. server_name = (char *) malloc (sizeof (char) * strlen(optarg));
  85. strcpy (server_name, optarg);
  86. options |= JackServerName;
  87. break;
  88. case 'u':
  89. use_uuid = 1;
  90. break;
  91. case 'f':
  92. force_connect = 1;
  93. break;
  94. case 'h':
  95. show_usage (my_name);
  96. return 1;
  97. break;
  98. case 'v':
  99. show_version (my_name);
  100. return 1;
  101. break;
  102. default:
  103. show_usage (my_name);
  104. return 1;
  105. break;
  106. }
  107. }
  108. connecting = disconnecting = FALSE;
  109. if (my_name == 0) {
  110. my_name = argv[0];
  111. } else {
  112. my_name ++;
  113. }
  114. if (strstr(my_name, "disconnect")) {
  115. disconnecting = 1;
  116. } else if (strstr(my_name, "connect")) {
  117. connecting = 1;
  118. } else {
  119. fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
  120. return 1;
  121. }
  122. if (argc < 3) {
  123. show_usage(my_name);
  124. return 1;
  125. }
  126. /* try to become a client of the JACK server */
  127. if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
  128. fprintf (stderr, "jack server not running?\n");
  129. return 1;
  130. }
  131. jack_set_port_connect_callback(client, port_connect_callback, NULL);
  132. /* find the two ports */
  133. if( use_uuid ) {
  134. char *tmpname;
  135. char *clientname;
  136. char *portname;
  137. tmpname = strdup( argv[argc-1] );
  138. portname = strchr( tmpname, ':' );
  139. portname[0] = '\0';
  140. portname+=1;
  141. clientname = jack_get_client_name_by_uuid( client, tmpname );
  142. if( clientname ) {
  143. snprintf( portA, sizeof(portA), "%s:%s", clientname, portname );
  144. jack_free( clientname );
  145. } else {
  146. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  147. }
  148. free( tmpname );
  149. tmpname = strdup( argv[argc-2] );
  150. portname = strchr( tmpname, ':' );
  151. portname[0] = '\0';
  152. portname+=1;
  153. clientname = jack_get_client_name_by_uuid( client, tmpname );
  154. if( clientname ) {
  155. snprintf( portB, sizeof(portB), "%s:%s", clientname, portname );
  156. jack_free( clientname );
  157. } else {
  158. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  159. }
  160. free( tmpname );
  161. } else {
  162. snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
  163. snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
  164. }
  165. if ((port1 = jack_port_by_name(client, portA)) == 0) {
  166. fprintf (stderr, "ERROR %s not a valid port\n", portA);
  167. goto exit;
  168. }
  169. if ((port2 = jack_port_by_name(client, portB)) == 0) {
  170. fprintf (stderr, "ERROR %s not a valid port\n", portB);
  171. goto exit;
  172. }
  173. port1_flags = jack_port_flags (port1);
  174. port2_flags = jack_port_flags (port2);
  175. if (port1_flags & JackPortIsInput) {
  176. if (port2_flags & JackPortIsOutput) {
  177. src_port = port2;
  178. dst_port = port1;
  179. }
  180. } else {
  181. if (port2_flags & JackPortIsInput) {
  182. src_port = port1;
  183. dst_port = port2;
  184. }
  185. }
  186. if (!src_port || !dst_port) {
  187. fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
  188. goto exit;
  189. }
  190. /* tell the JACK server that we are ready to roll */
  191. if (jack_activate (client)) {
  192. fprintf (stderr, "cannot activate client");
  193. goto exit;
  194. }
  195. /* connect the ports. Note: you can't do this before
  196. the client is activated (this may change in the future).
  197. */
  198. if (connecting) {
  199. if (!force_connect) {
  200. int fail = 0;
  201. /* Get the port type to know which proprty to query. */
  202. const char* src_port_type = jack_port_type(src_port);
  203. const char* dst_port_type = jack_port_type(dst_port);
  204. /* Get src's port content */
  205. char* src_type;
  206. if (strcmp(src_port_type, JACK_DEFAULT_AUDIO_TYPE) == 0)
  207. jack_get_property(jack_port_uuid(src_port), JACK_METADATA_SIGNAL_TYPE, &src_type, NULL);
  208. else if (strcmp(src_port_type, JACK_DEFAULT_MESSAGE_TYPE) == 0)
  209. jack_get_property(jack_port_uuid(src_port), JACK_METADATA_EVENT_TYPES, &src_type, NULL);
  210. else
  211. fail = 1;
  212. /* Get src's port content */
  213. char* dst_type;
  214. if (strcmp(dst_port_type, JACK_DEFAULT_AUDIO_TYPE) == 0)
  215. jack_get_property(jack_port_uuid(dst_port), JACK_METADATA_SIGNAL_TYPE, &dst_type, NULL);
  216. else if (strcmp(dst_port_type, JACK_DEFAULT_MESSAGE_TYPE) == 0)
  217. jack_get_property(jack_port_uuid(dst_port), JACK_METADATA_EVENT_TYPES, &dst_type, NULL);
  218. else
  219. fail = 1;
  220. /* Finally compare the ports */
  221. if (!fail)
  222. // TODO this does not handle if multiple content types are supported.
  223. fail = strcmp(src_type, dst_type);
  224. jack_free(src_type);
  225. jack_free(dst_type);
  226. if (fail) {
  227. fprintf (stderr, "cannot connect ports, since they may have different kinds of data. Run with --force to connect them anyways.\n");
  228. goto exit;
  229. }
  230. }
  231. if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  232. fprintf (stderr, "cannot connect client, already connected?\n");
  233. goto exit;
  234. }
  235. }
  236. if (disconnecting) {
  237. if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
  238. fprintf (stderr, "cannot disconnect client, already disconnected?\n");
  239. goto exit;
  240. }
  241. }
  242. // Wait for connection/disconnection to be effective
  243. while(!done) {
  244. #ifdef WIN32
  245. Sleep(10);
  246. #else
  247. usleep(10000);
  248. #endif
  249. }
  250. /* everything was ok, so setting exitcode to 0 */
  251. rc = 0;
  252. exit:
  253. jack_client_close (client);
  254. exit (rc);
  255. }