JMess.cpp 8.48 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
    JMess: A simple utility so save your jack-audio mess.

    Copyright (C) 2007 Juan-Pablo Caceres.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


/*
 * JMess.cpp
 */

#include "JMess.h"


//-------------------------------------------------------------------------------
/*! \brief Constructs a JMess object that has a jack client.
 *
 */
//-------------------------------------------------------------------------------
JMess::JMess()
{
  //Open a client connection to the JACK server.  Starting a
  //new server only to list its ports seems pointless, so we
  //specify JackNoStartServer. 
  mClient = jack_client_open ("lsp", JackNoStartServer, &mStatus);
  if (mClient == NULL) {
    if (mStatus & JackServerFailed) {
      cerr << "JACK server not running" << endl;
    } else {
      cerr << "jack_client_open() failed, " 
	   << "status = 0x%2.0x\n" << mStatus << endl;
    }
    exit(1);
  }
}


//-------------------------------------------------------------------------------
/*! \brief Distructor closes the jmess jack audio client.
 *
 */
//-------------------------------------------------------------------------------
JMess::~JMess()
{
  if (jack_client_close(mClient))
    cerr << "ERROR: Could not close the hidden jmess jack client." << endl;
}


//-------------------------------------------------------------------------------
/*! \brief Write an XML file with the name specified at xmlOutFile.
 *
 */
//-------------------------------------------------------------------------------
void JMess::writeOutput(QString xmlOutFile)
{
  QDomDocument jmess_xml;   QDomElement root;
  QDomElement connection;   QDomElement output;
  QDomElement input;        QDomText output_name;
  QDomText input_name;
  
  QVector<QString> OutputInput(2);

  this->setConnectedPorts();

  root = jmess_xml.createElement("jmess");
  for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
       it != mConnectedPorts.end(); ++it) {
    OutputInput = *it;
    //cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
    //cout << "Input ===> " <<qPrintable(OutputInput[1]) << endl;

    //Initialize XML elements
    connection = jmess_xml.createElement("connection");
    output = jmess_xml.createElement("output");
    input = jmess_xml.createElement("input");
    output_name = jmess_xml.createTextNode(OutputInput[0]);
    input_name = jmess_xml.createTextNode(OutputInput[1]);

    jmess_xml.appendChild(root);      root.appendChild(connection);
    connection.appendChild(output);   connection.appendChild(input);
    output.appendChild(output_name);  input.appendChild(input_name);
  }

  //Write output file
  QFile file(xmlOutFile);
  string answer = "";
  //Check for existing file first, and confirm before overwriting
  if (file.exists()) {
    while ((answer != "yes") && (answer != "no")) {
      cout << "WARNING: The File " <<qPrintable(xmlOutFile)
	   << " exists. Do you want to overwrite it? (yes/no): ";
      cin >> answer;
    }
  }
  else {
    answer = "yes";
  }

  if (answer == "yes") {
    if (!file.open(QIODevice::WriteOnly)) {
      cerr << "Cannot open file for writing: "
	   << qPrintable(file.errorString()) << endl;
      exit(1);
    }
    
    QTextStream out(&file);
    jmess_xml.save(out, Indent);
    cout << qPrintable(xmlOutFile) << " written." << endl;
  }
}
  

//-------------------------------------------------------------------------------
/*! \brief Set list of ouput ports that have connections.
 *
 */
//-------------------------------------------------------------------------------
void JMess::setConnectedPorts()
{
  mConnectedPorts.clear();

  const char **ports, **connections; //vector of ports and connections
  QVector<QString> OutputInput(2); //helper variable

  //Get active output ports.
  ports = jack_get_ports (mClient, NULL, NULL, JackPortIsOutput);
  
  for (unsigned int out_i = 0; ports[out_i]; ++out_i) {
    if ((connections = jack_port_get_all_connections 
	 (mClient, jack_port_by_name(mClient, ports[out_i]))) != 0) {
      for (unsigned int in_i = 0; connections[in_i]; ++in_i) {
	OutputInput[0] = ports[out_i];
	//cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
	OutputInput[1] = connections[in_i];
	//cout << "Input ===> " << qPrintable(OutputInput[1]) << endl;
	mConnectedPorts.append(OutputInput);
      }
    }
  }

  free(ports);
}


//-------------------------------------------------------------------------------
/*! \brief Disconnect all the clients.
 *
 */
//-------------------------------------------------------------------------------
void JMess::disconnectAll()
{
  QVector<QString> OutputInput(2);
  
  this->setConnectedPorts();
  
  for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
       it != mConnectedPorts.end(); ++it) {
    OutputInput = *it;
    
    if (jack_disconnect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
      cerr << "WARNING: port: " << qPrintable(OutputInput[0])
	   << "and port: " << qPrintable(OutputInput[1])
	   << " could not be disconnected.\n";
    }
  }

}


//-------------------------------------------------------------------------------
/*! \brief Parse the XML input file.
 *
 * Returns 0 on success, or 1 if the file has an incorrect format or cannot 
 * read the file.
 */
//-------------------------------------------------------------------------------
int JMess::parseXML(QString xmlInFile)
{
  mPortsToConnect.clear();
  QString errorStr;
  int errorLine;
  int errorColumn;
  
  QFile file(xmlInFile);
  if (!file.open(QIODevice::ReadOnly)) {
    cerr << "Cannot open file for reading: "
	 << qPrintable(file.errorString()) << endl;
    return 1;
  }

  QDomDocument doc;
  if (!doc.setContent(&file, true, &errorStr, &errorLine,
		      &errorColumn)) {
    cerr << "===================================================\n" 
	 << "Error parsing XML input file:\n"
	 << "Parse error at line " << errorLine
	 << ", column " << errorColumn << "\n"
	 << qPrintable(errorStr) << "\n"
	 << "===================================================\n";
    return 1;
  }
  
  QDomElement jmess = doc.documentElement();
  if (jmess.tagName() != "jmess") {
    cerr << "Error: Root tag should be <jmess>: "
	 << qPrintable(jmess.tagName()) << endl;
    return 1;
  }


  QVector<QString> OutputInput(2);
  //First check for <connection> tag
  for(QDomNode n_cntn = jmess.firstChild(); 
      !n_cntn.isNull(); n_cntn = n_cntn.nextSibling()) {
    QDomElement cntn = n_cntn.toElement();
    if (cntn.tagName() == "connection") {
      //Now check for ouput & input tag
      for(QDomNode n_sck = cntn.firstChild(); 
	  !n_sck.isNull(); n_sck = n_sck.nextSibling()) {
	QDomElement sck = n_sck.toElement();
	//cout << qPrintable(sck.tagName()) << endl;
	//cout << qPrintable(sck.text()) << endl;
	if (sck.tagName() == "output") {
	  OutputInput[0] = sck.text();
	}
	else if (sck.tagName() == "input") {
	  OutputInput[1] = sck.text();
	}
      }
      mPortsToConnect.append(OutputInput);
    }
  }

  return 0;
  
}


//-------------------------------------------------------------------------------
/*! \brief Connect ports specified in input XML file xmlInFile
 *
 */
//-------------------------------------------------------------------------------
void JMess::connectPorts(QString xmlInFile)
{
  QVector<QString> OutputInput(2);

  if ( !(this->parseXML(xmlInFile)) ) {  
    for (QVector<QVector<QString> >::iterator it = mPortsToConnect.begin();
	 it != mPortsToConnect.end(); ++it) {
      OutputInput = *it;

      if (jack_connect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
	//Display a warining only if the error is not because the ports are already
	//connected, in case the program doesn't display anyting.
	if (EEXIST != 
	    jack_connect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
	  cerr << "WARNING: port: " << qPrintable(OutputInput[0])
	       << "and port: " << qPrintable(OutputInput[1])
	       << " could not be connected.\n";
	}
      }
    }
  }

}