Merge branch 'master' into server_no_deadlock
[jack2.git] / example-clients / connect.c
bloba89e538a0a2d69908cf600f8d1a2a1dc4fc9e5e3
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 <jack/jack.h>
29 jack_port_t *input_port;
30 jack_port_t *output_port;
31 int connecting, disconnecting;
32 #define TRUE 1
33 #define FALSE 0
35 int
36 main (int argc, char *argv[])
38 jack_client_t* client = NULL;
39 char *my_name = strrchr(argv[0], '/');
40 connecting = disconnecting = FALSE;
41 if (my_name == 0) {
42 my_name = argv[0];
43 } else {
44 my_name ++;
47 printf("name %s\n", my_name);
49 if (strstr(my_name, "jack_disconnect")) {
50 disconnecting = TRUE;
51 } else
52 if (strstr(my_name, "jack_connect")) {
53 connecting = TRUE;
54 } else {
55 fprintf(stderr, "ERROR! client should be called jack_connect or jack_disconnect. client is called %s\n", my_name);
56 return 1;
59 if (argc != 3) {
60 fprintf (stderr, "usage: %s <src_port> <dst_port>\n", my_name);
61 fprintf(stderr, "The source port must be an output port of the source client.\n");
62 fprintf (stderr, "The destination port must be an input port of the destination client.\n");
63 return 1;
66 /* try to become a client of the JACK server */
68 if ((client = jack_client_open (my_name, JackNullOption, NULL)) == 0) {
69 fprintf (stderr, "jack server not running?\n");
70 return 1;
73 /* display the current sample rate. once the client is activated
74 (see below), you should rely on your own sample rate
75 callback (see above) for this value.
78 /* find the two ports */
80 if ((input_port = jack_port_by_name(client, argv[1])) == 0) {
81 fprintf (stderr, "ERROR %s not a valid port\n", argv[1]);
82 goto error;
84 if ((output_port = jack_port_by_name(client, argv[2])) == 0) {
85 fprintf (stderr, "ERROR %s not a valid port\n", argv[2]);
86 goto error;
89 /* tell the JACK server that we are ready to roll */
91 if (jack_activate (client)) {
92 fprintf (stderr, "cannot activate client");
93 goto error;
96 /* connect the ports. Note: you can't do this before
97 the client is activated (this may change in the future).
100 if (connecting) {
101 if (jack_connect(client, jack_port_name(input_port), jack_port_name(output_port))) {
102 fprintf (stderr, "cannot connect ports\n");
103 goto error;
106 if (disconnecting) {
107 if (jack_disconnect(client, jack_port_name(input_port), jack_port_name(output_port))) {
108 fprintf (stderr, "cannot disconnect ports\n");
109 goto error;
112 jack_deactivate (client);
113 jack_client_close (client);
114 return 0;
116 error:
117 if (client)
118 jack_client_close (client);
119 return 1;