Replace Util.pprint by Logs.pprint
[jack2.git] / example-clients / connect.c
blob5ed112b05013fe8d0c0a18b0915c0666c79db4b2
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 <math.h>
27 #include <getopt.h>
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;
35 #define TRUE 1
36 #define FALSE 0
38 void port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
40 done = 1;
43 void
44 show_version (char *my_name)
46 //fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
49 void
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");
61 int
62 main (int argc, char *argv[])
64 jack_client_t *client;
65 jack_status_t status;
66 char *server_name = NULL;
67 int c;
68 int option_index;
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;
75 char portA[300];
76 char portB[300];
77 int use_uuid=0;
78 int connecting, disconnecting;
79 int port1_flags, port2_flags;
80 int rc = 1;
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' },
87 { 0, 0, 0, 0 }
90 while ((c = getopt_long (argc, argv, "s:hvu", long_options, &option_index)) >= 0) {
91 switch (c) {
92 case 's':
93 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
94 strcpy (server_name, optarg);
95 options |= JackServerName;
96 break;
97 case 'u':
98 use_uuid = 1;
99 break;
100 case 'h':
101 show_usage (my_name);
102 return 1;
103 break;
104 case 'v':
105 show_version (my_name);
106 return 1;
107 break;
108 default:
109 show_usage (my_name);
110 return 1;
111 break;
115 connecting = disconnecting = FALSE;
116 if (my_name == 0) {
117 my_name = argv[0];
118 } else {
119 my_name ++;
122 if (strstr(my_name, "disconnect")) {
123 disconnecting = 1;
124 } else if (strstr(my_name, "connect")) {
125 connecting = 1;
126 } else {
127 fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
128 return 1;
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");
137 return 1;
140 jack_set_port_connect_callback(client, port_connect_callback, NULL);
142 /* find the two ports */
144 if( use_uuid ) {
145 char *tmpname;
146 char *clientname;
147 char *portname;
148 tmpname = strdup( argv[argc-1] );
149 portname = strchr( tmpname, ':' );
150 portname[0] = '\0';
151 portname+=1;
152 clientname = jack_get_client_name_by_uuid( client, tmpname );
153 if( clientname ) {
155 snprintf( portA, sizeof(portA), "%s:%s", clientname, portname );
156 jack_free( clientname );
157 } else {
158 snprintf( portA, sizeof(portA), "%s", argv[argc-1] );
160 free( tmpname );
162 tmpname = strdup( argv[argc-2] );
163 portname = strchr( tmpname, ':' );
164 portname[0] = '\0';
165 portname+=1;
166 clientname = jack_get_client_name_by_uuid( client, tmpname );
167 if( clientname ) {
168 snprintf( portB, sizeof(portB), "%s:%s", clientname, portname );
169 jack_free( clientname );
170 } else {
171 snprintf( portB, sizeof(portB), "%s", argv[argc-2] );
174 free( tmpname );
176 } else {
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);
182 goto exit;
184 if ((port2 = jack_port_by_name(client, portB)) == 0) {
185 fprintf (stderr, "ERROR %s not a valid port\n", portB);
186 goto exit;
189 port1_flags = jack_port_flags (port1);
190 port2_flags = jack_port_flags (port2);
192 if (port1_flags & JackPortIsInput) {
193 if (port2_flags & JackPortIsOutput) {
194 src_port = port2;
195 dst_port = port1;
197 } else {
198 if (port2_flags & JackPortIsInput) {
199 src_port = port1;
200 dst_port = port2;
204 if (!src_port || !dst_port) {
205 fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
206 goto exit;
209 /* tell the JACK server that we are ready to roll */
210 if (jack_activate (client)) {
211 fprintf (stderr, "cannot activate client");
212 goto exit;
215 /* connect the ports. Note: you can't do this before
216 the client is activated (this may change in the future).
219 if (connecting) {
220 if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
221 fprintf (stderr, "cannot connect client, already connected?\n");
222 goto exit;
225 if (disconnecting) {
226 if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
227 fprintf (stderr, "cannot disconnect client, already disconnected?\n");
228 goto exit;
232 // Wait for connection/disconnection to be effective
233 while(!done) {
234 #ifdef WIN32
235 Sleep(10);
236 #else
237 usleep(10000);
238 #endif
241 /* everything was ok, so setting exitcode to 0 */
242 rc = 0;
244 exit:
245 jack_client_close (client);
246 exit (rc);