2 Copyright (C) 2002 Jeremy Hall
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <jack/jack.h>
29 #include <jack/session.h>
34 volatile int done
= 0;
36 void port_connect_callback(jack_port_id_t a
, jack_port_id_t b
, int connect
, void* arg
)
42 show_version (char *my_name
)
44 //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
48 show_usage (char *my_name
)
50 show_version (my_name
);
51 fprintf (stderr
, "\nusage: %s [options] port1 port2\n", my_name
);
52 fprintf (stderr
, "Connects two JACK ports together.\n\n");
53 fprintf (stderr
, " -s, --server <name> Connect to the jack server named <name>\n");
54 fprintf (stderr
, " -v, --version Output version information and exit\n");
55 fprintf (stderr
, " -h, --help Display this help message\n\n");
56 fprintf (stderr
, "For more information see http://jackaudio.org/\n");
60 main (int argc
, char *argv
[])
62 jack_client_t
*client
;
64 char *server_name
= NULL
;
67 jack_options_t options
= JackNoStartServer
;
68 char *my_name
= strrchr(argv
[0], '/');
69 jack_port_t
*src_port
= 0;
70 jack_port_t
*dst_port
= 0;
71 jack_port_t
*port1
= 0;
72 jack_port_t
*port2
= 0;
76 int connecting
, disconnecting
;
77 int port1_flags
, port2_flags
;
80 struct option long_options
[] = {
81 { "server", 1, 0, 's' },
82 { "help", 0, 0, 'h' },
83 { "version", 0, 0, 'v' },
84 { "uuid", 0, 0, 'u' },
88 while ((c
= getopt_long (argc
, argv
, "s:hvu", long_options
, &option_index
)) >= 0) {
91 server_name
= (char *) malloc (sizeof (char) * (strlen(optarg
) + 1));
92 strcpy (server_name
, optarg
);
93 options
|= JackServerName
;
103 show_version (my_name
);
107 show_usage (my_name
);
113 connecting
= disconnecting
= FALSE
;
120 if (strstr(my_name
, "disconnect")) {
122 } else if (strstr(my_name
, "connect")) {
125 fprintf(stderr
, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name
);
134 /* try to become a client of the JACK server */
136 if ((client
= jack_client_open (my_name
, options
, &status
, server_name
)) == 0) {
137 fprintf (stderr
, "jack server not running?\n");
141 jack_set_port_connect_callback(client
, port_connect_callback
, NULL
);
143 /* find the two ports */
149 tmpname
= strdup( argv
[argc
-1] );
150 portname
= strchr( tmpname
, ':' );
153 clientname
= jack_get_client_name_by_uuid( client
, tmpname
);
156 snprintf( portA
, sizeof(portA
), "%s:%s", clientname
, portname
);
157 jack_free( clientname
);
159 snprintf( portA
, sizeof(portA
), "%s", argv
[argc
-1] );
163 tmpname
= strdup( argv
[argc
-2] );
164 portname
= strchr( tmpname
, ':' );
167 clientname
= jack_get_client_name_by_uuid( client
, tmpname
);
169 snprintf( portB
, sizeof(portB
), "%s:%s", clientname
, portname
);
170 jack_free( clientname
);
172 snprintf( portB
, sizeof(portB
), "%s", argv
[argc
-2] );
178 snprintf( portA
, sizeof(portA
), "%s", argv
[argc
-1] );
179 snprintf( portB
, sizeof(portB
), "%s", argv
[argc
-2] );
181 if ((port1
= jack_port_by_name(client
, portA
)) == 0) {
182 fprintf (stderr
, "ERROR %s not a valid port\n", portA
);
185 if ((port2
= jack_port_by_name(client
, portB
)) == 0) {
186 fprintf (stderr
, "ERROR %s not a valid port\n", portB
);
190 port1_flags
= jack_port_flags (port1
);
191 port2_flags
= jack_port_flags (port2
);
193 if (port1_flags
& JackPortIsInput
) {
194 if (port2_flags
& JackPortIsOutput
) {
199 if (port2_flags
& JackPortIsInput
) {
205 if (!src_port
|| !dst_port
) {
206 fprintf (stderr
, "arguments must include 1 input port and 1 output port\n");
210 /* tell the JACK server that we are ready to roll */
211 if (jack_activate (client
)) {
212 fprintf (stderr
, "cannot activate client");
216 /* connect the ports. Note: you can't do this before
217 the client is activated (this may change in the future).
221 if (jack_connect(client
, jack_port_name(src_port
), jack_port_name(dst_port
))) {
222 fprintf (stderr
, "cannot connect client, already connected?\n");
227 if (jack_disconnect(client
, jack_port_name(src_port
), jack_port_name(dst_port
))) {
228 fprintf (stderr
, "cannot disconnect client, already disconnected?\n");
233 // Wait for connection/disconnection to be effective
242 /* everything was ok, so setting exitcode to 0 */
246 jack_client_close (client
);