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>
31 jack_port_t
*input_port
;
32 jack_port_t
*output_port
;
33 int connecting
, disconnecting
;
34 volatile int done
= 0;
38 void port_connect_callback(jack_port_id_t a
, jack_port_id_t b
, int connect
, void* arg
)
44 show_version (char *my_name
)
46 //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
50 show_usage (char *my_name
)
52 show_version (my_name
);
53 fprintf (stderr
, "\nusage: %s [options] port1 port2\n", my_name
);
54 fprintf (stderr
, "Connects two JACK ports together.\n\n");
55 fprintf (stderr
, " -s, --server <name> Connect to the jack server named <name>\n");
56 fprintf (stderr
, " -v, --version Output version information and exit\n");
57 fprintf (stderr
, " -h, --help Display this help message\n\n");
58 fprintf (stderr
, "For more information see http://jackaudio.org/\n");
62 main (int argc
, char *argv
[])
64 jack_client_t
*client
;
66 char *server_name
= NULL
;
69 jack_options_t options
= JackNoStartServer
;
70 char *my_name
= strrchr(argv
[0], '/');
71 jack_port_t
*src_port
= 0;
72 jack_port_t
*dst_port
= 0;
73 jack_port_t
*port1
= 0;
74 jack_port_t
*port2
= 0;
78 int connecting
, disconnecting
;
79 int port1_flags
, port2_flags
;
82 struct option long_options
[] = {
83 { "server", 1, 0, 's' },
84 { "help", 0, 0, 'h' },
85 { "version", 0, 0, 'v' },
86 { "uuid", 0, 0, 'u' },
90 while ((c
= getopt_long (argc
, argv
, "s:hvu", long_options
, &option_index
)) >= 0) {
93 server_name
= (char *) malloc (sizeof (char) * strlen(optarg
));
94 strcpy (server_name
, optarg
);
95 options
|= JackServerName
;
101 show_usage (my_name
);
105 show_version (my_name
);
109 show_usage (my_name
);
115 connecting
= disconnecting
= FALSE
;
122 if (strstr(my_name
, "disconnect")) {
124 } else if (strstr(my_name
, "connect")) {
127 fprintf(stderr
, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name
);
136 /* try to become a client of the JACK server */
138 if ((client
= jack_client_open (my_name
, options
, &status
, server_name
)) == 0) {
139 fprintf (stderr
, "JACK server not running?\n");
143 jack_set_port_connect_callback(client
, port_connect_callback
, NULL
);
145 /* find the two ports */
151 tmpname
= strdup( argv
[argc
-1] );
152 portname
= strchr( tmpname
, ':' );
155 clientname
= jack_get_client_name_by_uuid( client
, tmpname
);
158 snprintf( portA
, sizeof(portA
), "%s:%s", clientname
, portname
);
159 jack_free( clientname
);
161 snprintf( portA
, sizeof(portA
), "%s", argv
[argc
-1] );
165 tmpname
= strdup( argv
[argc
-2] );
166 portname
= strchr( tmpname
, ':' );
169 clientname
= jack_get_client_name_by_uuid( client
, tmpname
);
171 snprintf( portB
, sizeof(portB
), "%s:%s", clientname
, portname
);
172 jack_free( clientname
);
174 snprintf( portB
, sizeof(portB
), "%s", argv
[argc
-2] );
180 snprintf( portA
, sizeof(portA
), "%s", argv
[argc
-1] );
181 snprintf( portB
, sizeof(portB
), "%s", argv
[argc
-2] );
183 if ((port1
= jack_port_by_name(client
, portA
)) == 0) {
184 fprintf (stderr
, "ERROR %s not a valid port\n", portA
);
187 if ((port2
= jack_port_by_name(client
, portB
)) == 0) {
188 fprintf (stderr
, "ERROR %s not a valid port\n", portB
);
192 port1_flags
= jack_port_flags (port1
);
193 port2_flags
= jack_port_flags (port2
);
195 if (port1_flags
& JackPortIsInput
) {
196 if (port2_flags
& JackPortIsOutput
) {
201 if (port2_flags
& JackPortIsInput
) {
207 if (!src_port
|| !dst_port
) {
208 fprintf (stderr
, "arguments must include 1 input port and 1 output port\n");
212 /* tell the JACK server that we are ready to roll */
213 if (jack_activate (client
)) {
214 fprintf (stderr
, "cannot activate client");
218 /* connect the ports. Note: you can't do this before
219 the client is activated (this may change in the future).
223 if (jack_connect(client
, jack_port_name(src_port
), jack_port_name(dst_port
))) {
224 fprintf (stderr
, "cannot connect client, already connected?\n");
229 if (jack_disconnect(client
, jack_port_name(src_port
), jack_port_name(dst_port
))) {
230 fprintf (stderr
, "cannot disconnect client, already disconnected?\n");
235 // Wait for connection/disconnection to be effective
244 /* everything was ok, so setting exitcode to 0 */
248 jack_client_close (client
);