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
);
131 if (argc
< 3) show_usage(my_name
);
133 /* try to become a client of the JACK server */
135 if ((client
= jack_client_open (my_name
, options
, &status
, server_name
)) == 0) {
136 fprintf (stderr
, "JACK server not running?\n");
140 jack_set_port_connect_callback(client
, port_connect_callback
, NULL
);
142 /* find the two ports */
148 tmpname
= strdup( argv
[argc
-1] );
149 portname
= strchr( tmpname
, ':' );
152 clientname
= jack_get_client_name_by_uuid( client
, tmpname
);
155 snprintf( portA
, sizeof(portA
), "%s:%s", clientname
, portname
);
156 jack_free( clientname
);
158 snprintf( portA
, sizeof(portA
), "%s", argv
[argc
-1] );
162 tmpname
= strdup( argv
[argc
-2] );
163 portname
= strchr( tmpname
, ':' );
166 clientname
= jack_get_client_name_by_uuid( client
, tmpname
);
168 snprintf( portB
, sizeof(portB
), "%s:%s", clientname
, portname
);
169 jack_free( clientname
);
171 snprintf( portB
, sizeof(portB
), "%s", argv
[argc
-2] );
177 snprintf( portA
, sizeof(portA
), "%s", argv
[argc
-1] );
178 snprintf( portB
, sizeof(portB
), "%s", argv
[argc
-2] );
180 if ((port1
= jack_port_by_name(client
, portA
)) == 0) {
181 fprintf (stderr
, "ERROR %s not a valid port\n", portA
);
184 if ((port2
= jack_port_by_name(client
, portB
)) == 0) {
185 fprintf (stderr
, "ERROR %s not a valid port\n", portB
);
189 port1_flags
= jack_port_flags (port1
);
190 port2_flags
= jack_port_flags (port2
);
192 if (port1_flags
& JackPortIsInput
) {
193 if (port2_flags
& JackPortIsOutput
) {
198 if (port2_flags
& JackPortIsInput
) {
204 if (!src_port
|| !dst_port
) {
205 fprintf (stderr
, "arguments must include 1 input port and 1 output port\n");
209 /* tell the JACK server that we are ready to roll */
210 if (jack_activate (client
)) {
211 fprintf (stderr
, "cannot activate client");
215 /* connect the ports. Note: you can't do this before
216 the client is activated (this may change in the future).
220 if (jack_connect(client
, jack_port_name(src_port
), jack_port_name(dst_port
))) {
221 fprintf (stderr
, "cannot connect client, already connected?\n");
226 if (jack_disconnect(client
, jack_port_name(src_port
), jack_port_name(dst_port
))) {
227 fprintf (stderr
, "cannot disconnect client, already disconnected?\n");
232 // Wait for connection/disconnection to be effective
241 /* everything was ok, so setting exitcode to 0 */
245 jack_client_close (client
);