JackGraphManager.cpp

00001 /*
00002 Copyright (C) 2001 Paul Davis
00003 Copyright (C) 2004-2006 Grame  
00004 
00005 This program is free software; you can redistribute it and/or modify
00006   it under the terms of the GNU General Public License as published by
00007   the Free Software Foundation; either version 2 of the License, or
00008   (at your option) any later version.
00009 
00010   This program is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013   GNU General Public License for more details.
00014 
00015   You should have received a copy of the GNU General Public License
00016   along with this program; if not, write to the Free Software
00017   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #include "JackGraphManager.h"
00021 #include "JackConstants.h"
00022 #include <assert.h>
00023 #include <stdlib.h>
00024 #include <algorithm>
00025 #include <regex.h>
00026 
00027 namespace Jack
00028 {
00029 
00030 static inline jack_nframes_t MAX(jack_nframes_t a, jack_nframes_t b)
00031 {
00032     return (a < b) ? b : a;
00033 }
00034 
00035 static void AssertPort(jack_port_id_t port_index)
00036 {
00037     if (port_index >= PORT_NUM) {
00038         JackLog("JackGraphManager::AssertPort port_index = %ld\n", port_index);
00039         assert(port_index < PORT_NUM);
00040     }
00041 }
00042 
00043 static void AssertBufferSize(jack_nframes_t buffer_size)
00044 {
00045     if (buffer_size > BUFFER_SIZE_MAX) {
00046         JackLog("JackGraphManager::AssertBufferSize frames = %ld\n", buffer_size);
00047         assert(buffer_size <= BUFFER_SIZE_MAX);
00048     }
00049 }
00050 
00051 JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
00052 {
00053     AssertPort(port_index);
00054     return &fPortArray[port_index];
00055 }
00056 
00057 float* JackGraphManager::GetBuffer(jack_port_id_t port_index)
00058 {
00059     return fPortArray[port_index].GetBuffer();
00060 }
00061 
00062 // RT, client
00063 int JackGraphManager::GetConnectionsNum(jack_port_id_t port_index)
00064 {
00065     JackConnectionManager* manager = ReadCurrentState();
00066     return manager->Connections(port_index);
00067 }
00068 
00069 // Server
00070 void JackGraphManager::InitRefNum(int refnum)
00071 {
00072     JackConnectionManager* manager = WriteNextStateStart();
00073     manager->InitRefNum(refnum);
00074     WriteNextStateStop();
00075 }
00076 
00077 // RT
00078 void JackGraphManager::RunCurrentGraph()
00079 {
00080     JackConnectionManager* manager = ReadCurrentState();
00081     manager->ResetGraph(fClientTiming);
00082 }
00083 
00084 // RT
00085 bool JackGraphManager::RunNextGraph()
00086 {
00087     bool res;
00088     JackConnectionManager* manager = TrySwitchState(&res);
00089     manager->ResetGraph(fClientTiming);
00090     return res;
00091 }
00092 
00093 // RT
00094 bool JackGraphManager::IsFinishedGraph()
00095 {
00096     JackConnectionManager* manager = ReadCurrentState();
00097     return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
00098 }
00099 
00100 // RT
00101 int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro** table)
00102 {
00103     JackConnectionManager* manager = ReadCurrentState();
00104     return manager->ResumeRefNum(control, table, fClientTiming);
00105 }
00106 
00107 // RT
00108 int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro** table, long usec)
00109 {
00110     JackConnectionManager* manager = ReadCurrentState();
00111     return manager->SuspendRefNum(control, table, fClientTiming, usec);
00112 }
00113 
00114 JackClientTiming* JackGraphManager::GetClientTiming(int ref)
00115 {
00116         return &fClientTiming[ref];
00117 }
00118 
00119 // Server
00120 void JackGraphManager::DirectConnect(int ref1, int ref2)
00121 {
00122     JackConnectionManager* manager = WriteNextStateStart();
00123     manager->DirectConnect(ref1, ref2);
00124     JackLog("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld\n", CurIndex(fCounter), ref1, ref2);
00125     WriteNextStateStop();
00126 }
00127 
00128 // Server
00129 void JackGraphManager::DirectDisconnect(int ref1, int ref2)
00130 {
00131     JackConnectionManager* manager = WriteNextStateStart();
00132     manager->DirectDisconnect(ref1, ref2);
00133     JackLog("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld\n", CurIndex(fCounter), ref1, ref2);
00134     WriteNextStateStop();
00135 }
00136 
00137 // Server
00138 bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
00139 {
00140     JackConnectionManager* manager = ReadCurrentState();
00141     return manager->IsDirectConnection(ref1, ref2);
00142 }
00143 
00144 // RT
00145 void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size)
00146 {
00147     AssertPort(port_index);
00148     AssertBufferSize(buffer_size);
00149 
00150     JackConnectionManager* manager = ReadCurrentState();
00151     JackPort* port = GetPort(port_index);
00152 
00153     if (!port->IsUsed()) {
00154         // This happens when a port has just been unregistered and is still used by the RT code.
00155         JackLog("JackGraphManager::GetBuffer : port = %ld is released state\n", port_index);
00156         return GetBuffer(0); // port_index 0 is not used
00157     }
00158 
00159     // Output port
00160     if (port->fFlags & JackPortIsOutput) {
00161         return (port->fTied != NO_PORT) ? GetBuffer(port->fTied, buffer_size) : GetBuffer(port_index);
00162     }
00163 
00164     // Input port
00165     jack_int_t len = manager->Connections(port_index);
00166 
00167     if (len == 0) {  // No connections: return a zero-filled buffer
00168         float* buffer = GetBuffer(port_index);
00169         memset(buffer, 0, buffer_size * sizeof(float)); // Clear buffer
00170         return buffer;
00171     } else if (len == 1) {       // One connection: use zero-copy mode - just pass the buffer of the connected (output) port.
00172         assert(manager->GetPort(port_index, 0) != port_index); // Check recursion
00173         return GetBuffer(manager->GetPort(port_index, 0), buffer_size);
00174     } else {  // Multiple connections
00175         const jack_int_t* connections = manager->GetConnections(port_index);
00176         float* mixbuffer = GetBuffer(port_index);
00177         jack_port_id_t src_index;
00178         float* buffer;
00179 
00180         // Copy first buffer
00181         src_index = connections[0];
00182         AssertPort(src_index);
00183         buffer = (float*)GetBuffer(src_index, buffer_size);
00184         memcpy(mixbuffer, buffer, buffer_size * sizeof(float));
00185 
00186         // Mix remaining buffers
00187         for (int i = 1; (i < CONNECTION_NUM) && ((src_index = connections[i]) != EMPTY); i++) {
00188             AssertPort(src_index);
00189             buffer = (float*)GetBuffer(src_index, buffer_size);
00190             JackPort::MixBuffer(mixbuffer, buffer, buffer_size);
00191         }
00192         return mixbuffer;
00193     }
00194 }
00195 
00196 // Server
00197 int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
00198 {
00199     AssertPort(port_index);
00200     JackPort* port = GetPort(port_index);
00201 
00211     port->RequestMonitor(onoff);
00212 
00213     const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
00214     if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags  & JackPortIsInput) ?
00215         jack_port_id_t src_index;
00216         for (int i = 0; (i < CONNECTION_NUM) && ((src_index = connections[i]) != EMPTY); i++) {
00217             // XXX much worse things will happen if there is a feedback loop !!!
00218             RequestMonitor(src_index, onoff);
00219         }
00220     }
00221 
00222     return 0;
00223 }
00224 
00225 // Server
00226 jack_nframes_t JackGraphManager::GetTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
00227 {
00228     const jack_int_t* connections = manager->GetConnections(port_index);
00229     jack_nframes_t latency = GetPort(port_index)->GetLatency();
00230     jack_nframes_t max_latency = 0;
00231     jack_port_id_t dst_index;
00232 
00233     if (hop_count > 8)
00234         return latency;
00235 
00236     for (int i = 0; (i < CONNECTION_NUM) && ((dst_index = connections[i]) != EMPTY); i++) {
00237         if (src_port_index != dst_index) {
00238             AssertPort(dst_index);
00239             JackPort* dst_port = GetPort(dst_index);
00240             jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
00241                                           ? dst_port->GetLatency()
00242                                           : GetTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
00243             max_latency = MAX(max_latency, this_latency);
00244         }
00245     }
00246 
00247     return max_latency + latency;
00248 }
00249 
00250 // Server
00251 jack_nframes_t JackGraphManager::GetTotalLatency(jack_port_id_t port_index)
00252 {
00253     UInt16 cur_index;
00254     UInt16 next_index;
00255     jack_nframes_t total_latency;
00256     AssertPort(port_index);
00257     JackLog("JackGraphManager::GetTotalLatency port_index = %ld\n", port_index);
00258 
00259     do {
00260         cur_index = GetCurrentIndex();
00261         total_latency = GetTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
00262         next_index = GetCurrentIndex();
00263     } while (cur_index != next_index); // Until a coherent state has been read
00264 
00265     return total_latency;
00266 }
00267 
00268 // Server
00269 jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, JackPortFlags flags)
00270 {
00271     jack_port_id_t port_index;
00272 
00273     // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
00274     for (port_index = FIRST_AVAILABLE_PORT; port_index < PORT_NUM; port_index++) {
00275         JackPort* port = GetPort(port_index);
00276         if (!port->IsUsed()) {
00277             JackLog("JackGraphManager::AllocatePortAux port_index = %ld name = %s\n", port_index, port_name);
00278             port->Allocate(refnum, port_name, flags);
00279             break;
00280         }
00281     }
00282 
00283     return (port_index < PORT_NUM) ? port_index : NO_PORT;
00284 }
00285 
00286 // Server
00287 jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, JackPortFlags flags)
00288 {
00289     JackConnectionManager* manager = WriteNextStateStart();
00290     jack_port_id_t port_index = AllocatePortAux(refnum, port_name, flags);
00291 
00292     if (port_index != NO_PORT) {
00293         int res;
00294         if (flags & JackPortIsOutput) {
00295             res = manager->AddOutputPort(refnum, port_index);
00296         } else {
00297             res = manager->AddInputPort(refnum, port_index);
00298         }
00299                 // Insertion failure
00300         if (res < 0) {
00301             JackPort* port = GetPort(port_index);
00302             assert(port);
00303             port->Release();
00304             port_index = NO_PORT;
00305         }
00306     }
00307 
00308     WriteNextStateStop();
00309     return port_index;
00310 }
00311 
00312 // Server
00313 int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
00314 {
00315     JackConnectionManager* manager = WriteNextStateStart();
00316     JackPort* port = GetPort(port_index);
00317     int res;
00318 
00319     if (port->fFlags & JackPortIsOutput) {
00320         DisconnectAllOutput(port_index);
00321         res = manager->RemoveOutputPort(refnum, port_index);
00322     } else {
00323         DisconnectAllInput(port_index);
00324         res = manager->RemoveInputPort(refnum, port_index);
00325     }
00326 
00327         port->Release();
00328     WriteNextStateStop();
00329     return res;
00330 }
00331 
00332 // Server
00333 void JackGraphManager::RemoveAllPorts(int refnum)
00334 {
00335     JackLog("JackGraphManager::RemoveAllPorts ref = %ld\n", refnum);
00336     JackConnectionManager* manager = WriteNextStateStart();
00337     jack_port_id_t port_index;
00338 
00339     // Warning : RemovePort shift port to left, thus we always remove the first port until the "input" table is empty
00340     const jack_int_t* input = manager->GetInputPorts(refnum);
00341     while ((port_index = input[0]) != EMPTY) {
00342         ReleasePort(refnum, port_index);
00343     }
00344 
00345     // Warning : RemovePort shift port to left, thus we always remove the first port until the "output" table is empty
00346     const jack_int_t* output = manager->GetOutputPorts(refnum);
00347     while ((port_index = output[0]) != EMPTY) {
00348         ReleasePort(refnum, port_index);
00349     }
00350 
00351     WriteNextStateStop();
00352 }
00353 
00354 // Server
00355 void JackGraphManager::DisconnectAllPorts(int refnum)
00356 {
00357     int i;
00358     JackLog("JackGraphManager::DisconnectAllPorts ref = %ld\n", refnum);
00359     JackConnectionManager* manager = WriteNextStateStart();
00360 
00361     const jack_int_t* input = manager->GetInputPorts(refnum);
00362     for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
00363         DisconnectAllInput(input[i]);
00364     }
00365 
00366     const jack_int_t* output = manager->GetOutputPorts(refnum);
00367     for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
00368         DisconnectAllOutput(output[i]);
00369     }
00370 
00371     WriteNextStateStop();
00372 }
00373 
00374 // Server
00375 void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
00376 {
00377     JackLog("JackGraphManager::DisconnectAllInput port_index = %ld \n", port_index);
00378     JackConnectionManager* manager = WriteNextStateStart();
00379 
00380     for (int i = 0; i < PORT_NUM; i++) {
00381         if (manager->IsConnected(i, port_index)) {
00382             JackLog("JackGraphManager::Disconnect i = %ld  port_index = %ld\n", i, port_index);
00383             Disconnect(i, port_index);
00384         }
00385     }
00386     WriteNextStateStop();
00387 }
00388 
00389 // Server
00390 void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
00391 {
00392     JackLog("JackGraphManager::DisconnectAllOutput port_index = %ld \n", port_index);
00393     JackConnectionManager* manager = WriteNextStateStart();
00394 
00395     const jack_int_t* connections = manager->GetConnections(port_index);
00396     while (connections[0] != EMPTY) {
00397         Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
00398     }
00399     WriteNextStateStop();
00400 }
00401 
00402 // Server
00403 int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
00404 {
00405     AssertPort(port_index);
00406 
00407     JackPort* port = GetPort(port_index);
00408     if (port->fFlags & JackPortIsOutput) {
00409         DisconnectAllOutput(port_index);
00410     } else {
00411         DisconnectAllInput(port_index);
00412     }
00413     return 0;
00414 }
00415 
00416 // Server
00417 void JackGraphManager::Activate(int refnum)
00418 {
00419         DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
00420     DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
00421 }
00422 
00423 /*
00424         Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
00425         (thus unactivated) state may happen where the client is still checked for its end.
00426 */
00427 
00428 // Server
00429 void JackGraphManager::Deactivate(int refnum)
00430 {
00431         DisconnectAllPorts(refnum);
00432         
00433         // Disconnect only when needed
00434     if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
00435         DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
00436     } else {
00437         JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
00438     }
00439 
00440         // Disconnect only when needed
00441     if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
00442         DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
00443     } else {
00444         JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
00445     }
00446 }
00447 
00448 // Server
00449 int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
00450 {
00451     AssertPort(port_index);
00452     JackConnectionManager* manager = WriteNextStateStart();
00453     int res = manager->GetInputRefNum(port_index);
00454     WriteNextStateStop();
00455     return res;
00456 }
00457 
00458 // Server
00459 int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
00460 {
00461     AssertPort(port_index);
00462     JackConnectionManager* manager = WriteNextStateStart();
00463     int res = manager->GetOutputRefNum(port_index);
00464     WriteNextStateStop();
00465     return res;
00466 }
00467 
00468 int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
00469 {
00470     JackConnectionManager* manager = WriteNextStateStart();
00471     JackLog("JackGraphManager::Connect port_src = %ld port_dst = %ld\n", port_src, port_dst);
00472     bool in_use_src = GetPort(port_src)->fInUse;
00473     bool in_use_dst = GetPort(port_dst)->fInUse;
00474     int res = 0;
00475 
00476     if (!in_use_src || !in_use_dst) {
00477         if (!in_use_src)
00478             jack_error("JackGraphManager::Connect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
00479         if (!in_use_dst)
00480             jack_error("JackGraphManager::Connect: port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
00481         res = -1;
00482         goto end;
00483     }
00484     if (manager->IsConnected(port_src, port_dst)) {
00485         jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
00486         res = EEXIST;
00487         goto end;
00488     }
00489 
00490     res = manager->Connect(port_src, port_dst);
00491     if (res < 0) {
00492         jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
00493         goto end;
00494     }
00495     manager->Connect(port_dst, port_src);
00496     if (res < 0) {
00497         jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
00498         goto end;
00499     }
00500 
00501     if (manager->IsLoopPath(port_src, port_dst)) {
00502         JackLog("JackGraphManager::Connect: LOOP detected\n");
00503         manager->IncFeedbackConnection(port_src, port_dst);
00504     } else {
00505         manager->IncDirectConnection(port_src, port_dst);
00506     }
00507 
00508 end:
00509     WriteNextStateStop();
00510     return res;
00511 }
00512 
00513 // Server
00514 int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
00515 {
00516     JackConnectionManager* manager = WriteNextStateStart();
00517     JackLog("JackGraphManager::Disconnect port_src = %ld port_dst = %ld\n", port_src, port_dst);
00518     bool in_use_src = GetPort(port_src)->fInUse;
00519     bool in_use_dst = GetPort(port_dst)->fInUse;
00520     int res = 0;
00521 
00522     if (!in_use_src || !in_use_dst) {
00523         if (!in_use_src)
00524             jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
00525         if (!in_use_dst)
00526             jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
00527         res = -1;
00528         goto end;
00529     }
00530     if (!manager->IsConnected(port_src, port_dst)) {
00531         jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
00532         res = -1;
00533         goto end;
00534     }
00535 
00536     manager->Disconnect(port_src, port_dst);
00537     if (res < 0) {
00538         jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
00539         goto end;
00540     }
00541     manager->Disconnect(port_dst, port_src);
00542     if (res < 0) {
00543         jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
00544         goto end;
00545     }
00546 
00547     if (manager->IsFeedbackConnection(port_src, port_dst)) {
00548         JackLog("JackGraphManager::Disconnect: FEEDBACK removed\n");
00549         manager->DecFeedbackConnection(port_src, port_dst);
00550     } else {
00551         manager->DecDirectConnection(port_src, port_dst);
00552     }
00553 
00554 end:
00555     WriteNextStateStop();
00556     return res;
00557 }
00558 
00559 // Client
00560 int JackGraphManager::ConnectedTo(jack_port_id_t port_src, const char* port_name)
00561 {
00562     JackLog("JackGraphManager::ConnectedTo port_src = %ld port_name = %s\n", port_src, port_name);
00563     JackConnectionManager* manager = ReadCurrentState();
00564     jack_port_id_t port_dst;
00565 
00566     if ((port_dst = GetPort(port_name)) == NO_PORT) {
00567         jack_error("Unknown destination port port_name = %s", port_name);
00568         return 0;
00569     } else {
00570         return manager->IsConnected(port_src, port_dst);
00571     }
00572 }
00573 
00574 // Server
00575 int JackGraphManager::CheckPort(jack_port_id_t port_index)
00576 {
00577     JackPort* port = GetPort(port_index);
00578 
00579     if (port->fLocked) {
00580         jack_error("Port %s is locked against connection changes", port->fName);
00581         return -1;
00582     } else {
00583         return 0;
00584     }
00585 }
00586 
00587 int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
00588 {
00589     JackPort* src = GetPort(port_src);
00590     JackPort* dst = GetPort(port_dst);
00591 
00592     if ((dst->Flags() & JackPortIsInput) == 0) {
00593         jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
00594         return -1;
00595     }
00596 
00597     if ((src->Flags() & JackPortIsOutput) == 0) {
00598         jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
00599         return -1;
00600     }
00601 
00602     if (src->fLocked) {
00603         jack_error("Source port %s is locked against connection changes", src->fName);
00604         return -1;
00605     }
00606 
00607     if (dst->fLocked) {
00608         jack_error("Destination port %s is locked against connection changes", dst->fName);
00609         return -1;
00610     }
00611 
00612     return 0;
00613 }
00614 
00615 int JackGraphManager::CheckPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
00616 {
00617     JackLog("JackGraphManager::CheckConnect src_name = %s dst_name = %s\n", src_name, dst_name);
00618 
00619     if ((*port_src = GetPort(src_name)) == NO_PORT) {
00620         jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
00621         return -1;
00622     }
00623 
00624     if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
00625         jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
00626         return -1;
00627     }
00628 
00629     return CheckPorts(*port_src, *port_dst);
00630 }
00631 
00632 // Client : port array
00633 jack_port_id_t JackGraphManager::GetPort(const char* name)
00634 {
00635     for (int i = 0; i < PORT_NUM; i++) {
00636         JackPort* port = GetPort(i);
00637         if (port->IsUsed() && port->NameEquals(name))
00638             return i;
00639     }
00640     return NO_PORT;
00641 }
00642 
00647 // Client
00648 void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
00649 {
00650     const jack_int_t* connections = manager->GetConnections(port_index);
00651     jack_int_t index;
00652     int i;
00653 
00654     for (i = 0; (i < CONNECTION_NUM) && ((index = connections[i]) != EMPTY); i++) {
00655         JackPort* port = GetPort(index);
00656         res[i] = port->fName;
00657     }
00658 
00659     res[i] = NULL;
00660 }
00661 
00662 /*
00663         Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
00664         The operation is lock-free since there is no intermediate state in the write operation that could cause the
00665         read to loop forever.
00666 */
00667 
00668 // Client
00669 const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
00670 {
00671     const char** res = (const char**)malloc(sizeof(char*) * (CONNECTION_NUM + 1));
00672     UInt16 cur_index;
00673     UInt16 next_index;
00674     AssertPort(port_index);
00675 
00676     do {
00677         cur_index = GetCurrentIndex();
00678         GetConnectionsAux(ReadCurrentState(), res, port_index);
00679         next_index = GetCurrentIndex();
00680     } while (cur_index != next_index); // Until a coherent state has been read
00681 
00682     return res;
00683 }
00684 
00685 // Client
00686 const char** JackGraphManager::GetPortsAux(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
00687 {
00688     const char** matching_ports;
00689     unsigned long match_cnt = 0;
00690     regex_t port_regex;
00691     regex_t type_regex;
00692     bool matching;
00693 
00694     if (port_name_pattern && port_name_pattern[0]) {
00695         regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
00696     }
00697     if (type_name_pattern && type_name_pattern[0]) {
00698         regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
00699     }
00700 
00701     matching_ports = (const char**)malloc(sizeof(char*) * PORT_NUM);
00702 
00703     for (int i = 0; i < PORT_NUM; i++) {
00704         matching = true;
00705                 JackPort* port = GetPort(i);
00706 
00707         if (port->IsUsed()) {
00708 
00709             if (flags) {
00710                 if ((port->fFlags & flags) != flags) {
00711                     matching = false;
00712                 }
00713             }
00714 
00715             if (matching && port_name_pattern && port_name_pattern[0]) {
00716                 if (regexec(&port_regex, port->fName, 0, NULL, 0)) {
00717                     matching = false;
00718                 }
00719             }
00720 
00721             /*
00722             if (matching && type_name_pattern && type_name_pattern[0]) {
00723                 jack_port_type_id_t ptid = psp[i].ptype_id;
00724                 if (regexec (&type_regex,engine->port_types[ptid].type_name,0, NULL, 0)) {
00725                     matching = false;
00726                 }
00727             } 
00728             */
00729 
00730             // TO BE IMPROVED
00731             if (matching && type_name_pattern && type_name_pattern[0]) {
00732                 if (regexec(&type_regex, JACK_DEFAULT_AUDIO_TYPE, 0, NULL, 0)) {
00733                     matching = false;
00734                 }
00735             }
00736 
00737             if (matching) {
00738                 matching_ports[match_cnt++] = port->fName;
00739             }
00740         }
00741     }
00742 
00743     matching_ports[match_cnt] = 0;
00744 
00745     if (match_cnt == 0) {
00746         free(matching_ports);
00747         matching_ports = NULL;
00748     }
00749 
00750     return matching_ports;
00751 }
00752 
00753 // Client
00754 /*
00755         Check that the state was not changed during the read operation.
00756         The operation is lock-free since there is no intermediate state in the write operation that could cause the
00757         read to loop forever.
00758 */
00759 const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
00760 {
00761     const char** matching_ports = NULL;
00762     UInt16 cur_index;
00763     UInt16 next_index;
00764 
00765     do {
00766         cur_index = GetCurrentIndex();
00767         if (matching_ports) {
00768             free(matching_ports);
00769                         JackLog("JackGraphManager::GetPorts retry... \n");
00770                 }
00771         matching_ports = GetPortsAux(port_name_pattern, type_name_pattern, flags);
00772         next_index = GetCurrentIndex();
00773     } while (cur_index != next_index); // Until a coherent state has been read
00774 
00775     return matching_ports;
00776 }
00777 
00778 // Server
00779 void JackGraphManager::Save(JackConnectionManager* dst)
00780 {
00781     JackConnectionManager* manager = WriteNextStateStart();
00782     memcpy(dst, manager, sizeof(JackConnectionManager));
00783     WriteNextStateStop();
00784 }
00785 
00786 // Server
00787 void JackGraphManager::Restore(JackConnectionManager* src)
00788 {
00789     JackConnectionManager* manager = WriteNextStateStart();
00790     memcpy(manager, src, sizeof(JackConnectionManager));
00791     WriteNextStateStop();
00792 }
00793 
00794 } // end of namespace
00795 
00796 

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1