make jack_connect agnostic about the order of its port arguments (duh!)
[jack.git] / tools / connect.c
blob67a18cfc1ef01368e391ce96beddd47021b9449c
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 #define TRUE 1
32 #define FALSE 0
34 void
35 show_version (char *my_name)
37 fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n", my_name);
40 void
41 show_usage (char *my_name)
43 show_version (my_name);
44 fprintf (stderr, "\nusage: %s [options] port1 port2\n", my_name);
45 fprintf (stderr, "Connects two JACK ports together.\n\n");
46 fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
47 fprintf (stderr, " -v, --version Output version information and exit\n");
48 fprintf (stderr, " -h, --help Display this help message\n\n");
49 fprintf (stderr, "For more information see http://jackaudio.org/\n");
52 int
53 main (int argc, char *argv[])
55 jack_client_t *client;
56 jack_status_t status;
57 char *server_name = NULL;
58 int c;
59 int option_index;
60 jack_options_t options = JackNoStartServer;
61 char *my_name = strrchr(argv[0], '/');
62 jack_port_t *src_port = 0;
63 jack_port_t *dst_port = 0;
64 jack_port_t *port1 = 0;
65 jack_port_t *port2 = 0;
66 int connecting, disconnecting;
67 int port1_flags, port2_flags;
69 struct option long_options[] = {
70 { "server", 1, 0, 's' },
71 { "help", 0, 0, 'h' },
72 { "version", 0, 0, 'v' },
73 { 0, 0, 0, 0 }
76 while ((c = getopt_long (argc, argv, "s:hv", long_options, &option_index)) >= 0) {
77 switch (c) {
78 case 's':
79 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
80 strcpy (server_name, optarg);
81 options |= JackServerName;
82 break;
83 case 'h':
84 show_usage (my_name);
85 return 1;
86 break;
87 case 'v':
88 show_version (my_name);
89 return 1;
90 break;
91 default:
92 show_usage (my_name);
93 return 1;
94 break;
98 connecting = disconnecting = FALSE;
99 if (my_name == 0) {
100 my_name = argv[0];
101 } else {
102 my_name ++;
105 if (strstr(my_name, "disconnect")) {
106 disconnecting = 1;
107 } else if (strstr(my_name, "connect")) {
108 connecting = 1;
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 /* find the two ports */
125 if ((port1 = jack_port_by_name(client, argv[argc-1])) == 0) {
126 fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-1]);
127 return 1;
129 if ((port2 = jack_port_by_name(client, argv[argc-2])) == 0) {
130 fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-2]);
131 return 1;
134 port1_flags = jack_port_flags (port1);
135 port2_flags = jack_port_flags (port2);
137 if (port1_flags & JackPortIsInput) {
138 if (port2_flags & JackPortIsOutput) {
139 src_port = port2;
140 dst_port = port1;
142 } else {
143 if (port2_flags & JackPortIsInput) {
144 src_port = port1;
145 dst_port = port2;
149 if (!src_port || !dst_port) {
150 fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
151 return 1;
154 /* connect the ports. Note: you can't do this before
155 the client is activated (this may change in the future).
158 if (connecting) {
159 if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
160 fprintf (stderr, "cannot connect ports\n");
161 return 1;
164 if (disconnecting) {
165 if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
166 fprintf (stderr, "cannot disconnect ports\n");
167 return 1;
171 jack_client_close (client);
172 exit (0);