summaryrefslogtreecommitdiff
path: root/example-clients/connect.c
blob: 5ed112b05013fe8d0c0a18b0915c0666c79db4b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
    Copyright (C) 2002 Jeremy Hall

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <getopt.h>
#include <jack/jack.h>
#include <jack/session.h>

jack_port_t *input_port;
jack_port_t *output_port;
int connecting, disconnecting;
volatile int done = 0;
#define TRUE 1
#define FALSE 0

void port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
{
    done = 1;
}

void
show_version (char *my_name)
{
	//fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
}

void
show_usage (char *my_name)
{
	show_version (my_name);
	fprintf (stderr, "\nusage: %s [options] port1 port2\n", my_name);
	fprintf (stderr, "Connects two JACK ports together.\n\n");
	fprintf (stderr, "        -s, --server <name>   Connect to the jack server named <name>\n");
	fprintf (stderr, "        -v, --version         Output version information and exit\n");
	fprintf (stderr, "        -h, --help            Display this help message\n\n");
	fprintf (stderr, "For more information see http://jackaudio.org/\n");
}

int
main (int argc, char *argv[])
{
	jack_client_t *client;
	jack_status_t status;
	char *server_name = NULL;
	int c;
	int option_index;
	jack_options_t options = JackNoStartServer;
	char *my_name = strrchr(argv[0], '/');
	jack_port_t *src_port = 0;
	jack_port_t *dst_port = 0;
	jack_port_t *port1 = 0;
	jack_port_t *port2 = 0;
	char portA[300];
	char portB[300];
	int use_uuid=0;
	int connecting, disconnecting;
	int port1_flags, port2_flags;
	int rc = 1;

	struct option long_options[] = {
	    { "server", 1, 0, 's' },
	    { "help", 0, 0, 'h' },
	    { "version", 0, 0, 'v' },
	    { "uuid", 0, 0, 'u' },
	    { 0, 0, 0, 0 }
	};

	while ((c = getopt_long (argc, argv, "s:hvu", long_options, &option_index)) >= 0) {
		switch (c) {
		case 's':
			server_name = (char *) malloc (sizeof (char) * strlen(optarg));
			strcpy (server_name, optarg);
			options |= JackServerName;
			break;
		case 'u':
			use_uuid = 1;
			break;
		case 'h':
			show_usage (my_name);
			return 1;
			break;
		case 'v':
			show_version (my_name);
			return 1;
			break;
		default:
			show_usage (my_name);
			return 1;
			break;
		}
	}

	connecting = disconnecting = FALSE;
	if (my_name == 0) {
		my_name = argv[0];
	} else {
		my_name ++;
	}

	if (strstr(my_name, "disconnect")) {
		disconnecting = 1;
	} else if (strstr(my_name, "connect")) {
		connecting = 1;
	} else {
		fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
		return 1;
	}

	if (argc < 3) show_usage(my_name);

	/* try to become a client of the JACK server */

	if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
		fprintf (stderr, "JACK server not running?\n");
		return 1;
	}

    jack_set_port_connect_callback(client, port_connect_callback, NULL);

	/* find the two ports */

	if( use_uuid ) {
		char *tmpname;
		char *clientname;
		char *portname;
		tmpname = strdup( argv[argc-1] );
		portname = strchr( tmpname, ':' );
		portname[0] = '\0';
		portname+=1;
		clientname = jack_get_client_name_by_uuid( client, tmpname );
		if( clientname ) {

			snprintf( portA, sizeof(portA), "%s:%s", clientname, portname );
			jack_free( clientname );
		} else {
			snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
		}
		free( tmpname );

		tmpname = strdup( argv[argc-2] );
		portname = strchr( tmpname, ':' );
		portname[0] = '\0';
		portname+=1;
		clientname = jack_get_client_name_by_uuid( client, tmpname );
		if( clientname ) {
			snprintf( portB, sizeof(portB), "%s:%s", clientname, portname );
			jack_free( clientname );
		} else {
			snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
		}

		free( tmpname );

	} else {
		snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
		snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
	}
	if ((port1 = jack_port_by_name(client, portA)) == 0) {
		fprintf (stderr, "ERROR %s not a valid port\n", portA);
		goto exit;
    }
	if ((port2 = jack_port_by_name(client, portB)) == 0) {
		fprintf (stderr, "ERROR %s not a valid port\n", portB);
		goto exit;
    }

	port1_flags = jack_port_flags (port1);
	port2_flags = jack_port_flags (port2);

	if (port1_flags & JackPortIsInput) {
		if (port2_flags & JackPortIsOutput) {
			src_port = port2;
			dst_port = port1;
		}
	} else {
		if (port2_flags & JackPortIsInput) {
			src_port = port1;
			dst_port = port2;
		}
	}

	if (!src_port || !dst_port) {
		fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
		goto exit;
	}

    /* tell the JACK server that we are ready to roll */
    if (jack_activate (client)) {
        fprintf (stderr, "cannot activate client");
        goto exit;
    }

	/* connect the ports. Note: you can't do this before
	   the client is activated (this may change in the future).
	*/

	if (connecting) {
		if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
            fprintf (stderr, "cannot connect client, already connected?\n");
			goto exit;
		}
	}
	if (disconnecting) {
		if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
            fprintf (stderr, "cannot disconnect client, already disconnected?\n");
			goto exit;
		}
	}

    // Wait for connection/disconnection to be effective
    while(!done) {
    #ifdef WIN32
        Sleep(10);
    #else
        usleep(10000);
    #endif
    }

	/* everything was ok, so setting exitcode to 0 */
	rc = 0;

exit:
	jack_client_close (client);
	exit (rc);
}