ensure that client-side message buffer thread calls thread_init callback if/when...
[jack.git] / tools / connect.c
blob23189a36c8789657b3cb73902d54a53c1e971120
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;
68 int rc = 1;
70 struct option long_options[] = {
71 { "server", 1, 0, 's' },
72 { "help", 0, 0, 'h' },
73 { "version", 0, 0, 'v' },
74 { 0, 0, 0, 0 }
77 while ((c = getopt_long (argc, argv, "s:hv", long_options, &option_index)) >= 0) {
78 switch (c) {
79 case 's':
80 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
81 strcpy (server_name, optarg);
82 options |= JackServerName;
83 break;
84 case 'h':
85 show_usage (my_name);
86 return 1;
87 break;
88 case 'v':
89 show_version (my_name);
90 return 1;
91 break;
92 default:
93 show_usage (my_name);
94 return 1;
95 break;
99 connecting = disconnecting = FALSE;
100 if (my_name == 0) {
101 my_name = argv[0];
102 } else {
103 my_name ++;
106 if (strstr(my_name, "disconnect")) {
107 disconnecting = 1;
108 } else if (strstr(my_name, "connect")) {
109 connecting = 1;
110 } else {
111 fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
112 return 1;
115 if (argc < 3) show_usage(my_name);
117 /* try to become a client of the JACK server */
119 if ((client = jack_client_open (my_name, options, &status, server_name)) == 0) {
120 fprintf (stderr, "jack server not running?\n");
121 return 1;
124 /* find the two ports */
126 if ((port1 = jack_port_by_name(client, argv[argc-1])) == 0) {
127 fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-1]);
128 goto exit;
130 if ((port2 = jack_port_by_name(client, argv[argc-2])) == 0) {
131 fprintf (stderr, "ERROR %s not a valid port\n", argv[argc-2]);
132 goto exit;
135 port1_flags = jack_port_flags (port1);
136 port2_flags = jack_port_flags (port2);
138 if (port1_flags & JackPortIsInput) {
139 if (port2_flags & JackPortIsOutput) {
140 src_port = port2;
141 dst_port = port1;
143 } else {
144 if (port2_flags & JackPortIsInput) {
145 src_port = port1;
146 dst_port = port2;
150 if (!src_port || !dst_port) {
151 fprintf (stderr, "arguments must include 1 input port and 1 output port\n");
152 goto exit;
155 /* connect the ports. Note: you can't do this before
156 the client is activated (this may change in the future).
159 if (connecting) {
160 if (jack_connect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
161 goto exit;
164 if (disconnecting) {
165 if (jack_disconnect(client, jack_port_name(src_port), jack_port_name(dst_port))) {
166 goto exit;
170 /* everything was ok, so setting exitcode to 0 */
171 rc = 0;
173 exit:
174 jack_client_close (client);
175 exit (rc);