Fix build under mixed mode
[jack2.git] / common / JackConnectionManager.cpp
bloba0cc04391a08191b3f9279591ba8d3fa67375620
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackConnectionManager.h"
21 #include "JackClientControl.h"
22 #include "JackEngineControl.h"
23 #include "JackGlobals.h"
24 #include "JackError.h"
25 #include <set>
26 #include <iostream>
27 #include <assert.h>
29 namespace Jack
32 JackConnectionManager::JackConnectionManager()
34 int i;
35 jack_log("JackConnectionManager::InitConnections size = %ld ", sizeof(JackConnectionManager));
37 for (i = 0; i < PORT_NUM_MAX; i++) {
38 fConnection[i].Init();
41 fLoopFeedback.Init();
43 jack_log("JackConnectionManager::InitClients");
44 for (i = 0; i < CLIENT_NUM; i++) {
45 InitRefNum(i);
49 JackConnectionManager::~JackConnectionManager()
52 //--------------
53 // Internal API
54 //--------------
56 bool JackConnectionManager::IsLoopPathAux(int ref1, int ref2) const
58 jack_log("JackConnectionManager::IsLoopPathAux ref1 = %ld ref2 = %ld", ref1, ref2);
60 if (ref1 < GetEngineControl()->fDriverNum || ref2 < GetEngineControl()->fDriverNum) {
61 return false;
62 } else if (ref1 == ref2) { // Same refnum
63 return true;
64 } else {
65 jack_int_t output[CLIENT_NUM];
66 fConnectionRef.GetOutputTable(ref1, output);
68 if (fConnectionRef.IsInsideTable(ref2, output)) { // If ref2 is contained in the outputs of ref1
69 return true;
70 } else {
71 for (int i = 0; i < CLIENT_NUM && output[i] != EMPTY; i++) { // Otherwise recurse for all ref1 outputs
72 if (IsLoopPathAux(output[i], ref2)) {
73 return true; // Stop when a path is found
76 return false;
81 //--------------
82 // External API
83 //--------------
85 /*!
86 \brief Connect port_src to port_dst.
88 int JackConnectionManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
90 jack_log("JackConnectionManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
92 if (fConnection[port_src].AddItem(port_dst)) {
93 return 0;
94 } else {
95 jack_error("Connection table is full !!");
96 return -1;
101 \brief Disconnect port_src from port_dst.
103 int JackConnectionManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
105 jack_log("JackConnectionManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
107 if (fConnection[port_src].RemoveItem(port_dst)) {
108 return 0;
109 } else {
110 jack_error("Connection not found !!");
111 return -1;
116 \brief Check if port_src and port_dst are connected.
118 bool JackConnectionManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst) const
120 return fConnection[port_src].CheckItem(port_dst);
124 \brief Get the connection port array.
126 const jack_int_t* JackConnectionManager::GetConnections(jack_port_id_t port_index) const
128 return fConnection[port_index].GetItems();
131 //------------------------
132 // Client port management
133 //------------------------
136 \brief Add an input port to a client.
138 int JackConnectionManager::AddInputPort(int refnum, jack_port_id_t port_index)
140 if (fInputPort[refnum].AddItem(port_index)) {
141 jack_log("JackConnectionManager::AddInputPort ref = %ld port = %ld", refnum, port_index);
142 return 0;
143 } else {
144 jack_error("Maximum number of input ports is reached for application ref = %ld", refnum);
145 return -1;
150 \brief Add an output port to a client.
152 int JackConnectionManager::AddOutputPort(int refnum, jack_port_id_t port_index)
154 if (fOutputPort[refnum].AddItem(port_index)) {
155 jack_log("JackConnectionManager::AddOutputPort ref = %ld port = %ld", refnum, port_index);
156 return 0;
157 } else {
158 jack_error("Maximum number of output ports is reached for application ref = %ld", refnum);
159 return -1;
164 \brief Remove an input port from a client.
166 int JackConnectionManager::RemoveInputPort(int refnum, jack_port_id_t port_index)
168 jack_log("JackConnectionManager::RemoveInputPort ref = %ld port_index = %ld ", refnum, port_index);
170 if (fInputPort[refnum].RemoveItem(port_index)) {
171 return 0;
172 } else {
173 jack_error("Input port index = %ld not found for application ref = %ld", port_index, refnum);
174 return -1;
179 \brief Remove an output port from a client.
181 int JackConnectionManager::RemoveOutputPort(int refnum, jack_port_id_t port_index)
183 jack_log("JackConnectionManager::RemoveOutputPort ref = %ld port_index = %ld ", refnum, port_index);
185 if (fOutputPort[refnum].RemoveItem(port_index)) {
186 return 0;
187 } else {
188 jack_error("Output port index = %ld not found for application ref = %ld", port_index, refnum);
189 return -1;
194 \brief Get the input port array of a given refnum.
196 const jack_int_t* JackConnectionManager::GetInputPorts(int refnum)
198 return fInputPort[refnum].GetItems();
202 \brief Get the output port array of a given refnum.
204 const jack_int_t* JackConnectionManager::GetOutputPorts(int refnum)
206 return fOutputPort[refnum].GetItems();
210 \brief Init the refnum.
212 void JackConnectionManager::InitRefNum(int refnum)
214 fInputPort[refnum].Init();
215 fOutputPort[refnum].Init();
216 fConnectionRef.Init(refnum);
217 fInputCounter[refnum].SetValue(0);
221 \brief Reset all clients activation.
223 void JackConnectionManager::ResetGraph(JackClientTiming* timing)
225 // Reset activation counter : must be done *before* starting to resume clients
226 for (int i = 0; i < CLIENT_NUM; i++) {
227 fInputCounter[i].Reset();
228 timing[i].fStatus = NotTriggered;
233 \brief Wait on the input synchro.
235 int JackConnectionManager::SuspendRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing, long time_out_usec)
237 bool res;
238 if ((res = table[control->fRefNum].TimedWait(time_out_usec))) {
239 timing[control->fRefNum].fStatus = Running;
240 timing[control->fRefNum].fAwakeAt = GetMicroSeconds();
242 return (res) ? 0 : -1;
246 \brief Signal clients connected to the given client.
248 int JackConnectionManager::ResumeRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing)
250 jack_time_t current_date = GetMicroSeconds();
251 const jack_int_t* output_ref = fConnectionRef.GetItems(control->fRefNum);
252 int res = 0;
254 // Update state and timestamp of current client
255 timing[control->fRefNum].fStatus = Finished;
256 timing[control->fRefNum].fFinishedAt = current_date;
258 for (int i = 0; i < CLIENT_NUM; i++) {
260 // Signal connected clients or drivers
261 if (output_ref[i] > 0) {
263 // Update state and timestamp of destination clients
264 timing[i].fStatus = Triggered;
265 timing[i].fSignaledAt = current_date;
267 if (!fInputCounter[i].Signal(table + i, control)) {
268 jack_log("JackConnectionManager::ResumeRefNum error: ref = %ld output = %ld ", control->fRefNum, i);
269 res = -1;
274 return res;
277 static bool HasNoConnection(jack_int_t* table)
279 for (int ref = 0; ref < CLIENT_NUM; ref++) {
280 if (table[ref] > 0) return false;
282 return true;
285 // Using http://en.wikipedia.org/wiki/Topological_sorting
287 void JackConnectionManager::TopologicalSort(std::vector<jack_int_t>& sorted)
289 JackFixedMatrix<CLIENT_NUM>* tmp = new JackFixedMatrix<CLIENT_NUM>;
290 std::set<jack_int_t> level;
292 fConnectionRef.Copy(*tmp);
294 // Inputs of the graph
295 level.insert(AUDIO_DRIVER_REFNUM);
296 level.insert(FREEWHEEL_DRIVER_REFNUM);
298 while (level.size() > 0) {
299 jack_int_t refnum = *level.begin();
300 sorted.push_back(refnum);
301 level.erase(level.begin());
302 const jack_int_t* output_ref1 = tmp->GetItems(refnum);
303 for (int dst = 0; dst < CLIENT_NUM; dst++) {
304 if (output_ref1[dst] > 0) {
305 tmp->ClearItem(refnum, dst);
306 jack_int_t output_ref2[CLIENT_NUM];
307 tmp->GetOutputTable1(dst, output_ref2);
308 if (HasNoConnection(output_ref2)) {
309 level.insert(dst);
315 delete tmp;
319 \brief Increment the number of ports between 2 clients, if the 2 clients become connected, then the Activation counter is updated.
321 void JackConnectionManager::IncDirectConnection(jack_port_id_t port_src, jack_port_id_t port_dst)
323 int ref1 = GetOutputRefNum(port_src);
324 int ref2 = GetInputRefNum(port_dst);
326 assert(ref1 >= 0 && ref2 >= 0);
328 DirectConnect(ref1, ref2);
329 jack_log("JackConnectionManager::IncConnectionRef: ref1 = %ld ref2 = %ld", ref1, ref2);
333 \brief Decrement the number of ports between 2 clients, if the 2 clients become disconnected, then the Activation counter is updated.
335 void JackConnectionManager::DecDirectConnection(jack_port_id_t port_src, jack_port_id_t port_dst)
337 int ref1 = GetOutputRefNum(port_src);
338 int ref2 = GetInputRefNum(port_dst);
340 assert(ref1 >= 0 && ref2 >= 0);
342 DirectDisconnect(ref1, ref2);
343 jack_log("JackConnectionManager::DecConnectionRef: ref1 = %ld ref2 = %ld", ref1, ref2);
347 \brief Directly connect 2 reference numbers.
349 void JackConnectionManager::DirectConnect(int ref1, int ref2)
351 assert(ref1 >= 0 && ref2 >= 0);
353 if (fConnectionRef.IncItem(ref1, ref2) == 1) { // First connection between client ref1 and client ref2
354 jack_log("JackConnectionManager::DirectConnect first: ref1 = %ld ref2 = %ld", ref1, ref2);
355 fInputCounter[ref2].IncValue();
360 \brief Directly disconnect 2 reference numbers.
362 void JackConnectionManager::DirectDisconnect(int ref1, int ref2)
364 assert(ref1 >= 0 && ref2 >= 0);
366 if (fConnectionRef.DecItem(ref1, ref2) == 0) { // Last connection between client ref1 and client ref2
367 jack_log("JackConnectionManager::DirectDisconnect last: ref1 = %ld ref2 = %ld", ref1, ref2);
368 fInputCounter[ref2].DecValue();
373 \brief Returns the connections state between 2 refnum.
375 bool JackConnectionManager::IsDirectConnection(int ref1, int ref2) const
377 assert(ref1 >= 0 && ref2 >= 0);
378 return (fConnectionRef.GetItemCount(ref1, ref2) > 0);
382 \brief Get the client refnum of a given input port.
384 int JackConnectionManager::GetInputRefNum(jack_port_id_t port_index) const
386 for (int i = 0; i < CLIENT_NUM; i++) {
387 if (fInputPort[i].CheckItem(port_index)) {
388 return i;
392 return -1;
396 \brief Get the client refnum of a given output port.
398 int JackConnectionManager::GetOutputRefNum(jack_port_id_t port_index) const
400 for (int i = 0; i < CLIENT_NUM; i++) {
401 if (fOutputPort[i].CheckItem(port_index)) {
402 return i;
406 return -1;
410 \brief Test is a connection path exists between port_src and port_dst.
412 bool JackConnectionManager::IsLoopPath(jack_port_id_t port_src, jack_port_id_t port_dst) const
414 return IsLoopPathAux(GetInputRefNum(port_dst), GetOutputRefNum(port_src));
417 bool JackConnectionManager::IsFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst) const
419 return (fLoopFeedback.GetConnectionIndex(GetOutputRefNum(port_src), GetInputRefNum(port_dst)) >= 0);
422 bool JackConnectionManager::IncFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst)
424 int ref1 = GetOutputRefNum(port_src);
425 int ref2 = GetInputRefNum(port_dst);
427 // Add an activation connection in the other direction
428 jack_log("JackConnectionManager::IncFeedbackConnection ref1 = %ld ref2 = %ld", ref1, ref2);
429 assert(ref1 >= 0 && ref2 >= 0);
431 if (ref1 != ref2) {
432 DirectConnect(ref2, ref1);
435 return fLoopFeedback.IncConnection(ref1, ref2); // Add the feedback connection
438 bool JackConnectionManager::DecFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst)
440 int ref1 = GetOutputRefNum(port_src);
441 int ref2 = GetInputRefNum(port_dst);
443 // Remove an activation connection in the other direction
444 jack_log("JackConnectionManager::DecFeedbackConnection ref1 = %ld ref2 = %ld", ref1, ref2);
445 assert(ref1 >= 0 && ref2 >= 0);
447 if (ref1 != ref2) {
448 DirectDisconnect(ref2, ref1);
451 return fLoopFeedback.DecConnection(ref1, ref2); // Remove the feedback connection
454 } // end of namespace