1 // ------------------------------------------------------------------------
2 // jack-connections.cpp: Utility class to manage JACK port connections
3 // Copyright (C) 2008 Kai Vehmanen
4 //
5 // Attributes:
6 // eca-style-version: 3 (see Ecasound Programmer's Guide)
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // ------------------------------------------------------------------------
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string>
28 #include <iostream>
29
30 #include <jack/jack.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34
35 #include <kvu_numtostr.h>
36
37 #include "eca-logger.h"
38 #include "jack-connections.h"
39
40 using std::string;
41
JACK_CONNECTIONS(void)42 JACK_CONNECTIONS::JACK_CONNECTIONS(void)
43 {
44 }
45
~JACK_CONNECTIONS(void)46 JACK_CONNECTIONS::~JACK_CONNECTIONS(void)
47 {
48 }
49
priv_prepare(void)50 static jack_client_t *priv_prepare(void)
51 {
52 int pid = getpid();
53 std::string clntname = "libecasound-ctrl-" + kvu_numtostr(pid);
54 jack_client_t *client = jack_client_open(clntname.c_str(), JackNullOption, NULL);
55 return client;
56 }
57
priv_cleanup(jack_client_t * client)58 static void priv_cleanup(jack_client_t *client)
59 {
60 jack_client_close(client);
61 }
62
connect(const char * src,const char * dest)63 bool JACK_CONNECTIONS::connect(const char* src, const char* dest)
64 {
65
66 int result = -1;
67 jack_client_t *client = priv_prepare();
68 if (client != 0) {
69 result = jack_connect(client, src, dest);
70
71 ECA_LOG_MSG(ECA_LOGGER::user_objects,
72 std::string("Connected JACK ports ") +
73 src +
74 " and " +
75 dest +
76 " with result of " +
77 kvu_numtostr(result));
78
79 priv_cleanup(client);
80
81 }
82
83 return result == 0;
84 }
85
disconnect(const char * src,const char * dest)86 bool JACK_CONNECTIONS::disconnect(const char* src, const char* dest)
87 {
88 int result = -1;
89 jack_client_t *client = priv_prepare();
90 if (client != 0) {
91 result = jack_disconnect(client, src, dest);
92
93 ECA_LOG_MSG(ECA_LOGGER::user_objects,
94 std::string("Connected JACK ports ") +
95 src +
96 " and " +
97 dest +
98 " with result of " +
99 kvu_numtostr(result));
100
101 priv_cleanup(client);
102 }
103
104 return result == 0;
105 }
106
list_connections(std::string * output)107 bool JACK_CONNECTIONS::list_connections(std::string* output)
108 {
109 jack_client_t *client = priv_prepare();
110 if (client != 0) {
111 const char **next, **ports =
112 jack_get_ports(client, NULL, NULL, 0);
113
114 if (ports) {
115
116 *output += "\n";
117
118 for (next = ports; *next; next++) {
119 jack_port_t *port;
120
121 *output += string(*next);
122
123 port = jack_port_by_name(client, *next);
124
125 const char **nextconn, **conns =
126 jack_port_get_all_connections(client, port);
127
128 if (conns) {
129 for(nextconn = conns; *nextconn; nextconn++) {
130 *output += string("\n\t") + string(*nextconn) + string("\n");
131 }
132 free(conns);
133 }
134 else {
135 *output += "\n";
136 }
137 }
138 free(ports);
139 }
140
141 priv_cleanup(client);
142 }
143
144 return client != 0;
145 }
146