Ignore versions on windows installer, always replacing files
[jack2.git] / tools / connect.c
blobfc43b5e9f8d3cdc189d5d767f38257c86700f3cf
1 /*
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.
19 #include <stdio.h>
20 #include <errno.h>
21 #ifndef WIN32
22 #include <unistd.h>
23 #endif
24 #include <string.h>
25 #include <stdlib.h>
26 #include <getopt.h>
28 #include <jack/jack.h>
29 #include <jack/session.h>
31 #define TRUE 1
32 #define FALSE 0
34 volatile int done = 0;
36 void port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
38 done = 1;
41 void
42 show_version (char *my_name)
44 //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
47 void
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");
59 int
60 main (int argc, char *argv[])
62 jack_client_t *client;
63 jack_status_t status;
64 char *server_name = NULL;
65 int c;
66 int option_index;
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;
73 char portA[300];
74 char portB[300];
75 int use_uuid=0;
76 int connecting, disconnecting;
77 int port1_flags, port2_flags;
78 int rc = 1;
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' },
85 { 0, 0, 0, 0 }
88 while ((c = getopt_long (argc, argv, "s:hvu", long_options, &option_index)) >= 0) {
89 switch (c) {
90 case 's':
91 server_name = (char *) malloc (sizeof (char) * (strlen(optarg) + 1));
92 strcpy (server_name, optarg);
93 options |= JackServerName;
94 break;
95 case 'u':
96 use_uuid = 1;
97 break;
98 case 'h':
99 show_usage (my_name);
100 return 1;
101 break;
102 case 'v':
103 show_version (my_name);
104 return 1;
105 break;
106 default:
107 show_usage (my_name);
108 return 1;
109 break;
113 connecting = disconnecting = FALSE;
114 if (my_name == 0) {
115 my_name = argv[0];
116 } else {
117 my_name ++;
120 if (strstr(my_name, "disconnect")) {
121 disconnecting = 1;
122 } else if (strstr(my_name, "connect")) {
123 connecting = 1;
124 } else {
125 fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
126 return 1;
129 if (argc < 3) {
130 show_usage(my_name);
131 return 1;
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");
138 return 1;
141 jack_set_port_connect_callback(client, port_connect_callback, NULL);
143 /* find the two ports */
145 if( use_uuid ) {
146 char *tmpname;
147 char *clientname;
148 char *portname;
149 tmpname = strdup( argv[argc-1] );
150 portname = strchr( tmpname, ':' );
151 portname[0] = '\0';
152 portname+=1;
153 clientname = jack_get_client_name_by_uuid( client, tmpname );
154 if( clientname ) {
156 snprintf( portA, sizeof(portA), "%s:%s", clientname, portname );
157 jack_free( clientname );
158 } else {
159 snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
161 free( tmpname );
163 tmpname = strdup( argv[argc-2] );
164 portname = strchr( tmpname, ':' );
165 portname[0] = '\0';
166 portname+=1;
167 clientname = jack_get_client_name_by_uuid( client, tmpname );
168 if( clientname ) {
169 snprintf( portB, sizeof(portB), "%s:%s", clientname, portname );
170 jack_free( clientname );
171 } else {
172 snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
175 free( tmpname );
177 } else {
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);
183 goto exit;
185 if ((port2 = jack_port_by_name(client, portB)) == 0) {
186 fprintf (stderr, "ERROR %s not a valid port\n", portB);
187 goto exit;
190 port1_flags = jack_port_flags (port1);
191 port2_flags = jack_port_flags (port2);
193 if (port1_flags & JackPortIsInput) {
194 if (port2_flags & JackPortIsOutput) {
195 src_port = port2;
196 dst_port = port1;
198 } else {
199 if (port2_flags & JackPortIsInput) {
200 src_port = port1;
201 dst_port = port2;
205 if (!src_port || !dst_port) {
206 fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
207 goto exit;
210 /* tell the JACK server that we are ready to roll */
211 if (jack_activate (client)) {
212 fprintf (stderr, "cannot activate client");
213 goto exit;
216 /* connect the ports. Note: you can't do this before
217 the client is activated (this may change in the future).
220 if (connecting) {
221 if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
222 fprintf (stderr, "cannot connect client, already connected?\n");
223 goto exit;
226 if (disconnecting) {
227 if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
228 fprintf (stderr, "cannot disconnect client, already disconnected?\n");
229 goto exit;
233 // Wait for connection/disconnection to be effective
234 while(!done) {
235 #ifdef WIN32
236 Sleep(10);
237 #else
238 usleep(10000);
239 #endif
242 /* everything was ok, so setting exitcode to 0 */
243 rc = 0;
245 exit:
246 jack_client_close (client);
247 exit (rc);