compile and configuration fixes from OSX but useful everywhere
[jack.git] / tools / connect.c
blobbb8a99636847454889f410c5c262cacf685468dc
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.
20 #include <stdio.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <getopt.h>
27 #include <config.h>
29 #include <jack/jack.h>
31 jack_port_t *input_port;
32 jack_port_t *output_port;
33 int connecting, disconnecting;
34 #define TRUE 1
35 #define FALSE 0
37 void
38 show_version (char *my_name)
40 fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
43 void
44 show_usage (char *my_name)
46 show_version (my_name);
47 fprintf (stderr, "\nusage: %s [options] <src_port> <dst_port>\n", my_name);
48 fprintf (stderr, "Connects two JACK ports together.\n\n");
49 fprintf (stderr, " The source port must be an output port of the source client.\n");
50 fprintf (stderr, " The destination port must be an input port of the destination client.\n");
51 fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
52 fprintf (stderr, " -v, --version Output version information and exit\n");
53 fprintf (stderr, " -h, --help Display this help message\n\n");
54 fprintf (stderr, "For more information see http://jackaudio.org/\n");
57 int
58 main (int argc, char *argv[])
60 jack_client_t *client;
61 jack_status_t status;
62 char *server_name = NULL;
63 int c;
64 int option_index;
65 jack_options_t options = JackNoStartServer;
66 char *my_name = strrchr(argv[0], '/');
68 struct option long_options[] = {
69 { "server", 1, 0, 's' },
70 { "help", 0, 0, 'h' },
71 { "version", 0, 0, 'v' },
72 { 0, 0, 0, 0 }
75 while ((c = getopt_long (argc, argv, "s:AclLphvt", long_options, &option_index)) >= 0) {
76 switch (c) {
77 case 's':
78 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
79 strcpy (server_name, optarg);
80 options |= JackServerName;
81 break;
82 case 'h':
83 show_usage (my_name);
84 return 1;
85 break;
86 case 'v':
87 show_version (my_name);
88 return 1;
89 break;
90 default:
91 show_usage (my_name);
92 return 1;
93 break;
97 connecting = disconnecting = FALSE;
98 if (my_name == 0) {
99 my_name = argv[0];
100 } else {
101 my_name ++;
104 if (strstr(my_name, "disconnect")) {
105 disconnecting = TRUE;
106 } else
107 if (strstr(my_name, "connect")) {
108 connecting = TRUE;
109 } else {
110 fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
111 return 1;
114 if (argc < 3) show_usage(my_name);
116 /* try to become a client of the JACK server */
118 if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
119 fprintf (stderr, "jack server not running?\n");
120 return 1;
123 /* display the current sample rate. once the client is activated
124 (see below), you should rely on your own sample rate
125 callback (see above) for this value.
128 printf ("engine sample rate: %" PRIu32 "\n",
129 jack_get_sample_rate (client));
131 /* find the two ports */
133 if ((input_port = jack_port_by_name(client, argv[argc-1])) == 0) {
134 fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-1]);
135 return 1;
137 if ((output_port = jack_port_by_name(client, argv[argc-2])) == 0) {
138 fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-2]);
139 return 1;
142 /* tell the JACK server that we are ready to roll */
144 if (jack_activate (client)) {
145 fprintf (stderr, "cannot activate client");
146 return 1;
149 /* connect the ports. Note: you can't do this before
150 the client is activated (this may change in the future).
153 /* jack_port_connect not implemented
154 if (jack_port_connect(client, input_port, output_port)) {
155 fprintf (stderr, "cannot connect ports\n");
158 if (connecting) {
159 if (jack_connect(client, jack_port_name(input_port), jack_port_name(output_port))) {
160 fprintf (stderr, "cannot connect ports\n");
161 return 1;
164 if (disconnecting) {
165 if (jack_disconnect(client, jack_port_name(input_port), jack_port_name(output_port))) {
166 fprintf (stderr, "cannot disconnect ports\n");
167 return 1;
171 jack_client_close (client);
172 exit (0);