00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021 #include <fstream>
00022 #include <assert.h>
00023
00024 #include "JackSystemDeps.h"
00025 #include "JackLockedEngine.h"
00026 #include "JackExternalClient.h"
00027 #include "JackInternalClient.h"
00028 #include "JackEngineControl.h"
00029 #include "JackClientControl.h"
00030 #include "JackServerGlobals.h"
00031 #include "JackGlobals.h"
00032 #include "JackChannel.h"
00033 #include "JackError.h"
00034
00035 namespace Jack
00036 {
00037
00038 #define AssertRefnum(ref) assert(ref >= 0 && ref < CLIENT_NUM);
00039
00040 JackEngine::JackEngine(JackGraphManager* manager,
00041 JackSynchro* table,
00042 JackEngineControl* control)
00043 {
00044 fGraphManager = manager;
00045 fSynchroTable = table;
00046 fEngineControl = control;
00047 for (int i = 0; i < CLIENT_NUM; i++)
00048 fClientTable[i] = NULL;
00049 }
00050
00051 JackEngine::~JackEngine()
00052 {
00053 jack_log("JackEngine::~JackEngine");
00054 }
00055
00056 int JackEngine::Open()
00057 {
00058 jack_log("JackEngine::Open");
00059
00060
00061 if (fChannel.Open(fEngineControl->fServerName) < 0) {
00062 jack_error("Cannot connect to server");
00063 return -1;
00064 } else {
00065 return 0;
00066 }
00067 }
00068
00069 int JackEngine::Close()
00070 {
00071 jack_log("JackEngine::Close");
00072 fChannel.Close();
00073
00074
00075 for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
00076 if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
00077 jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
00078 loadable_client->Close();
00079
00080 fClientTable[i] = NULL;
00081 delete loadable_client;
00082 } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
00083 jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
00084 external_client->Close();
00085
00086 fClientTable[i] = NULL;
00087 }
00088 }
00089
00090 return 0;
00091 }
00092
00093
00094
00095
00096
00097 int JackEngine::AllocateRefnum()
00098 {
00099 for (int i = 0; i < CLIENT_NUM; i++) {
00100 if (!fClientTable[i]) {
00101 jack_log("JackEngine::AllocateRefNum ref = %ld", i);
00102 return i;
00103 }
00104 }
00105 return -1;
00106 }
00107
00108 void JackEngine::ReleaseRefnum(int ref)
00109 {
00110 fClientTable[ref] = NULL;
00111
00112 if (fEngineControl->fTemporary) {
00113 int i;
00114 for (i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
00115 if (fClientTable[i])
00116 break;
00117 }
00118 if (i == CLIENT_NUM) {
00119
00120 jack_log("JackEngine::ReleaseRefnum server quit");
00121 fEngineControl->fTemporary = false;
00122 #ifndef WIN32
00123 exit(0);
00124 #endif
00125 }
00126 }
00127 }
00128
00129
00130
00131
00132
00133 void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
00134 {
00135 fLastSwitchUsecs = cur_cycle_begin;
00136 if (fGraphManager->RunNextGraph())
00137 fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
00138 fSignal.Signal();
00139 }
00140
00141 void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
00142 {
00143 if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs)
00144 CheckXRun(cur_cycle_begin);
00145 fGraphManager->RunCurrentGraph();
00146 }
00147
00148 bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
00149 {
00150 bool res = true;
00151
00152
00153 fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
00154
00155
00156 if (fGraphManager->IsFinishedGraph()) {
00157 ProcessNext(cur_cycle_begin);
00158 res = true;
00159 } else {
00160 jack_log("Process: graph not finished!");
00161 if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
00162 jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
00163 ProcessNext(cur_cycle_begin);
00164 res = true;
00165 } else {
00166 jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
00167 ProcessCurrent(cur_cycle_begin);
00168 res = false;
00169 }
00170 }
00171
00172
00173 fEngineControl->CycleEnd(fClientTable);
00174 return res;
00175 }
00176
00177
00178
00179
00180
00181
00182 void JackEngine::CheckXRun(jack_time_t callback_usecs)
00183 {
00184 for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
00185 JackClientInterface* client = fClientTable[i];
00186 if (client && client->GetClientControl()->fActive) {
00187 JackClientTiming* timing = fGraphManager->GetClientTiming(i);
00188 jack_client_state_t status = timing->fStatus;
00189 jack_time_t finished_date = timing->fFinishedAt;
00190
00191 if (status != NotTriggered && status != Finished) {
00192 jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
00193 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
00194 }
00195
00196 if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
00197 jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
00198 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
00199 }
00200 }
00201 }
00202 }
00203
00204
00205
00206
00207
00208 void JackEngine::NotifyClient(int refnum, int event, int sync, int value1, int value2)
00209 {
00210 JackClientInterface* client = fClientTable[refnum];
00211
00212
00213 if (!client) {
00214 jack_log("JackEngine::NotifyClient: client not available anymore");
00215 } else if (client->GetClientControl()->fCallback[event]) {
00216 if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
00217 jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
00218 } else {
00219 jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
00220 }
00221 }
00222
00223 void JackEngine::NotifyClients(int event, int sync, int value1, int value2)
00224 {
00225 for (int i = 0; i < CLIENT_NUM; i++) {
00226 JackClientInterface* client = fClientTable[i];
00227 if (client) {
00228 if (client->GetClientControl()->fCallback[event]) {
00229 if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
00230 jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
00231 } else {
00232 jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
00233 }
00234 }
00235 }
00236 }
00237
00238 int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
00239 {
00240 jack_log("JackEngine::NotifyAddClient: name = %s", name);
00241
00242 for (int i = 0; i < CLIENT_NUM; i++) {
00243 JackClientInterface* old_client = fClientTable[i];
00244 if (old_client) {
00245 if (old_client->ClientNotify(refnum, name, kAddClient, true, 0, 0) < 0) {
00246 jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
00247 return -1;
00248 }
00249 if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, 0, 0) < 0) {
00250 jack_error("NotifyAddClient new_client fails name = %s", name);
00251 return -1;
00252 }
00253 }
00254 }
00255
00256 return 0;
00257 }
00258
00259 void JackEngine::NotifyRemoveClient(const char* name, int refnum)
00260 {
00261
00262 for (int i = 0; i < CLIENT_NUM; i++) {
00263 JackClientInterface* client = fClientTable[i];
00264 if (client) {
00265 client->ClientNotify(refnum, name, kRemoveClient, true, 0, 0);
00266 }
00267 }
00268 }
00269
00270
00271 void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
00272 {
00273
00274 fEngineControl->ResetFrameTime(callback_usecs);
00275 fEngineControl->NotifyXRun(delayed_usecs);
00276 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
00277 }
00278
00279 void JackEngine::NotifyXRun(int refnum)
00280 {
00281 if (refnum == ALL_CLIENTS) {
00282 NotifyClients(kXRunCallback, false, 0, 0);
00283 } else {
00284 NotifyClient(refnum, kXRunCallback, false, 0, 0);
00285 }
00286 }
00287
00288 void JackEngine::NotifyGraphReorder()
00289 {
00290 NotifyClients(kGraphOrderCallback, false, 0, 0);
00291 }
00292
00293 void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size)
00294 {
00295 NotifyClients(kBufferSizeCallback, true, buffer_size, 0);
00296 }
00297
00298 void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
00299 {
00300 NotifyClients(kSampleRateCallback, true, sample_rate, 0);
00301 }
00302
00303 void JackEngine::NotifyFreewheel(bool onoff)
00304 {
00305 fEngineControl->fRealTime = !onoff;
00306 NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, 0, 0);
00307 }
00308
00309 void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
00310 {
00311 NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, port_index, 0);
00312 }
00313
00314 void JackEngine::NotifyPortRename(jack_port_id_t port)
00315 {
00316 NotifyClients(kPortRenameCallback, false, port, 0);
00317 }
00318
00319 void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
00320 {
00321 NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, src, dst);
00322 }
00323
00324 void JackEngine::NotifyActivate(int refnum)
00325 {
00326 NotifyClient(refnum, kActivateClient, true, 0, 0);
00327 }
00328
00329
00330
00331
00332
00333 int JackEngine::GetInternalClientName(int refnum, char* name_res)
00334 {
00335 AssertRefnum(refnum);
00336 JackClientInterface* client = fClientTable[refnum];
00337 if (client) {
00338 strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
00339 return 0;
00340 } else {
00341 return -1;
00342 }
00343 }
00344
00345 int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
00346 {
00347
00348 *status = 0;
00349
00350 for (int i = 0; i < CLIENT_NUM; i++) {
00351 JackClientInterface* client = fClientTable[i];
00352 if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
00353 jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
00354 *int_ref = i;
00355 return 0;
00356 }
00357 }
00358
00359 *status |= (JackNoSuchClient | JackFailure);
00360 return -1;
00361 }
00362
00363 int JackEngine::InternalClientUnload(int refnum, int* status)
00364 {
00365 AssertRefnum(refnum);
00366 JackClientInterface* client = fClientTable[refnum];
00367 if (client) {
00368 int res = client->Close();
00369 delete client;
00370 *status = 0;
00371 return res;
00372 } else {
00373 *status = (JackNoSuchClient | JackFailure);
00374 return -1;
00375 }
00376 }
00377
00378
00379
00380
00381
00382 int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
00383 {
00384
00385 *status = 0;
00386 strcpy(name_res, name);
00387
00388 jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
00389
00390 if (protocol != JACK_PROTOCOL_VERSION) {
00391 *status |= (JackFailure | JackVersionError);
00392 jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
00393 return -1;
00394 }
00395
00396 if (ClientCheckName(name)) {
00397
00398 *status |= JackNameNotUnique;
00399
00400 if (options & JackUseExactName) {
00401 jack_error("cannot create new client; %s already exists", name);
00402 *status |= JackFailure;
00403 return -1;
00404 }
00405
00406 if (GenerateUniqueName(name_res)) {
00407 *status |= JackFailure;
00408 return -1;
00409 }
00410 }
00411
00412 return 0;
00413 }
00414
00415 bool JackEngine::GenerateUniqueName(char* name)
00416 {
00417 int tens, ones;
00418 int length = strlen(name);
00419
00420 if (length > JACK_CLIENT_NAME_SIZE - 4) {
00421 jack_error("%s exists and is too long to make unique", name);
00422 return true;
00423 }
00424
00425
00426 name[length++] = '-';
00427 tens = length++;
00428 ones = length++;
00429 name[tens] = '0';
00430 name[ones] = '1';
00431 name[length] = '\0';
00432
00433 while (ClientCheckName(name)) {
00434 if (name[ones] == '9') {
00435 if (name[tens] == '9') {
00436 jack_error("client %s has 99 extra instances already", name);
00437 return true;
00438 }
00439 name[tens]++;
00440 name[ones] = '0';
00441 } else {
00442 name[ones]++;
00443 }
00444 }
00445 return false;
00446 }
00447
00448 bool JackEngine::ClientCheckName(const char* name)
00449 {
00450 for (int i = 0; i < CLIENT_NUM; i++) {
00451 JackClientInterface* client = fClientTable[i];
00452 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
00453 return true;
00454 }
00455
00456 return false;
00457 }
00458
00459 int JackEngine::GetClientPID(const char* name)
00460 {
00461 for (int i = 0; i < CLIENT_NUM; i++) {
00462 JackClientInterface* client = fClientTable[i];
00463 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
00464 return client->GetClientControl()->fPID;
00465 }
00466
00467 return 0;
00468 }
00469
00470 int JackEngine::GetClientRefNum(const char* name)
00471 {
00472 for (int i = 0; i < CLIENT_NUM; i++) {
00473 JackClientInterface* client = fClientTable[i];
00474 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
00475 return client->GetClientControl()->fRefNum;
00476 }
00477
00478 return -1;
00479 }
00480
00481
00482 int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
00483 {
00484 jack_log("JackEngine::ClientOpen: name = %s ", name);
00485
00486 int refnum = AllocateRefnum();
00487 if (refnum < 0) {
00488 jack_error("No more refnum available");
00489 return -1;
00490 }
00491
00492 JackExternalClient* client = new JackExternalClient();
00493
00494 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
00495 jack_error("Cannot allocate synchro");
00496 goto error;
00497 }
00498
00499 if (client->Open(name, pid, refnum, shared_client) < 0) {
00500 jack_error("Cannot open client");
00501 goto error;
00502 }
00503
00504 if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
00505
00506 jack_error("Driver is not running");
00507 goto error;
00508 }
00509
00510 fClientTable[refnum] = client;
00511
00512 if (NotifyAddClient(client, name, refnum) < 0) {
00513 jack_error("Cannot notify add client");
00514 goto error;
00515 }
00516
00517 fGraphManager->InitRefNum(refnum);
00518 fEngineControl->ResetRollingUsecs();
00519 *shared_engine = fEngineControl->GetShmIndex();
00520 *shared_graph_manager = fGraphManager->GetShmIndex();
00521 *ref = refnum;
00522 return 0;
00523
00524 error:
00525
00526 fSynchroTable[refnum].Destroy();
00527 fClientTable[refnum] = 0;
00528 client->Close();
00529 delete client;
00530 return -1;
00531 }
00532
00533
00534 int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
00535 {
00536 jack_log("JackEngine::ClientInternalNew: name = %s", name);
00537
00538 int refnum = AllocateRefnum();
00539 if (refnum < 0) {
00540 jack_error("No more refnum available");
00541 goto error;
00542 }
00543
00544 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
00545 jack_error("Cannot allocate synchro");
00546 goto error;
00547 }
00548
00549 if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
00550
00551 jack_error("Driver is not running");
00552 goto error;
00553 }
00554
00555 fClientTable[refnum] = client;
00556
00557 if (NotifyAddClient(client, name, refnum) < 0) {
00558 jack_error("Cannot notify add client");
00559 goto error;
00560 }
00561
00562 fGraphManager->InitRefNum(refnum);
00563 fEngineControl->ResetRollingUsecs();
00564 *shared_engine = fEngineControl;
00565 *shared_manager = fGraphManager;
00566 *ref = refnum;
00567 return 0;
00568
00569 error:
00570
00571 fSynchroTable[refnum].Destroy();
00572 fClientTable[refnum] = 0;
00573 return -1;
00574 }
00575
00576
00577 int JackEngine::ClientExternalClose(int refnum)
00578 {
00579 AssertRefnum(refnum);
00580 JackClientInterface* client = fClientTable[refnum];
00581
00582 if (client) {
00583 fEngineControl->fTransport.ResetTimebase(refnum);
00584 int res = ClientCloseAux(refnum, client, true);
00585 client->Close();
00586 delete client;
00587 return res;
00588 } else {
00589 return -1;
00590 }
00591 }
00592
00593
00594 int JackEngine::ClientInternalClose(int refnum, bool wait)
00595 {
00596 AssertRefnum(refnum);
00597 JackClientInterface* client = fClientTable[refnum];
00598 return (client) ? ClientCloseAux(refnum, client, wait) : -1;
00599 }
00600
00601 int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
00602 {
00603 jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
00604
00605
00606 jack_int_t ports[PORT_NUM_FOR_CLIENT];
00607 int i;
00608
00609 fGraphManager->GetInputPorts(refnum, ports);
00610 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
00611 PortUnRegister(refnum, ports[i]);
00612 }
00613
00614 fGraphManager->GetOutputPorts(refnum, ports);
00615 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
00616 PortUnRegister(refnum, ports[i]);
00617 }
00618
00619
00620 ReleaseRefnum(refnum);
00621
00622
00623 fGraphManager->RemoveAllPorts(refnum);
00624
00625
00626 if (wait) {
00627 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) {
00628 jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
00629 }
00630 }
00631
00632
00633 NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
00634
00635
00636 fSynchroTable[refnum].Destroy();
00637 fEngineControl->ResetRollingUsecs();
00638 return 0;
00639 }
00640
00641 int JackEngine::ClientActivate(int refnum, bool state)
00642 {
00643 AssertRefnum(refnum);
00644 JackClientInterface* client = fClientTable[refnum];
00645 assert(fClientTable[refnum]);
00646
00647 jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
00648 if (state)
00649 fGraphManager->Activate(refnum);
00650
00651
00652 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
00653 jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
00654 return -1;
00655 } else {
00656 NotifyActivate(refnum);
00657 return 0;
00658 }
00659 }
00660
00661
00662 int JackEngine::ClientDeactivate(int refnum)
00663 {
00664 AssertRefnum(refnum);
00665 JackClientInterface* client = fClientTable[refnum];
00666 if (client == NULL)
00667 return -1;
00668
00669 jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
00670
00671
00672 jack_int_t ports[PORT_NUM_FOR_CLIENT];
00673 int i;
00674
00675 fGraphManager->GetInputPorts(refnum, ports);
00676 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
00677 PortDisconnect(refnum, ports[i], ALL_PORTS);
00678 }
00679
00680 fGraphManager->GetOutputPorts(refnum, ports);
00681 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
00682 PortDisconnect(refnum, ports[i], ALL_PORTS);
00683 }
00684
00685 fGraphManager->Deactivate(refnum);
00686 fLastSwitchUsecs = 0;
00687
00688
00689 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
00690 jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
00691 return -1;
00692 } else {
00693 return 0;
00694 }
00695 }
00696
00697
00698
00699
00700
00701 int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
00702 {
00703 jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
00704 AssertRefnum(refnum);
00705 assert(fClientTable[refnum]);
00706
00707
00708 if (fGraphManager->GetPort(name) != NO_PORT) {
00709 jack_error("port_name \"%s\" already exists", name);
00710 return -1;
00711 }
00712
00713 *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
00714 if (*port_index != NO_PORT) {
00715 NotifyPortRegistation(*port_index, true);
00716 return 0;
00717 } else {
00718 return -1;
00719 }
00720 }
00721
00722 int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
00723 {
00724 jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
00725 AssertRefnum(refnum);
00726 assert(fClientTable[refnum]);
00727
00728
00729 PortDisconnect(refnum, port_index, ALL_PORTS);
00730
00731 if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
00732 NotifyPortRegistation(port_index, false);
00733 return 0;
00734 } else {
00735 return -1;
00736 }
00737 }
00738
00739 int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
00740 {
00741 jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
00742 AssertRefnum(refnum);
00743 jack_port_id_t port_src, port_dst;
00744
00745 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
00746 ? -1
00747 : PortConnect(refnum, port_src, port_dst);
00748 }
00749
00750 int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
00751 {
00752 jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
00753 AssertRefnum(refnum);
00754 JackClientInterface* client;
00755 int ref;
00756
00757 if (fGraphManager->CheckPorts(src, dst) < 0)
00758 return -1;
00759
00760 ref = fGraphManager->GetOutputRefNum(src);
00761 assert(ref >= 0);
00762 client = fClientTable[ref];
00763 assert(client);
00764 if (!client->GetClientControl()->fActive) {
00765 jack_error("Cannot connect ports owned by inactive clients:"
00766 " \"%s\" is not active", client->GetClientControl()->fName);
00767 return -1;
00768 }
00769
00770 ref = fGraphManager->GetInputRefNum(dst);
00771 assert(ref >= 0);
00772 client = fClientTable[ref];
00773 assert(client);
00774 if (!client->GetClientControl()->fActive) {
00775 jack_error("Cannot connect ports owned by inactive clients:"
00776 " \"%s\" is not active", client->GetClientControl()->fName);
00777 return -1;
00778 }
00779
00780 int res = fGraphManager->Connect(src, dst);
00781 if (res == 0)
00782 NotifyPortConnect(src, dst, true);
00783 return res;
00784 }
00785
00786 int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
00787 {
00788 jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
00789 AssertRefnum(refnum);
00790 jack_port_id_t port_src, port_dst;
00791
00792 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
00793 ? -1
00794 : PortDisconnect(refnum, port_src, port_dst);
00795 }
00796
00797 int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
00798 {
00799 jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
00800 AssertRefnum(refnum);
00801
00802 if (dst == ALL_PORTS) {
00803
00804 jack_int_t connections[CONNECTION_NUM_FOR_PORT];
00805 fGraphManager->GetConnections(src, connections);
00806
00807 JackPort* port = fGraphManager->GetPort(src);
00808 int ret = 0;
00809 if (port->GetFlags() & JackPortIsOutput) {
00810 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
00811 if (PortDisconnect(refnum, src, connections[i]) != 0) {
00812 ret = -1;
00813 }
00814 }
00815 } else {
00816 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
00817 if (PortDisconnect(refnum, connections[i], src) != 0) {
00818 ret = -1;
00819 }
00820 }
00821 }
00822
00823 return ret;
00824 } else if (fGraphManager->CheckPorts(src, dst) < 0) {
00825 return -1;
00826 } else if (fGraphManager->Disconnect(src, dst) == 0) {
00827
00828 NotifyPortConnect(src, dst, false);
00829 return 0;
00830 } else {
00831 return -1;
00832 }
00833 }
00834
00835 int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
00836 {
00837 AssertRefnum(refnum);
00838 fGraphManager->GetPort(port)->SetName(name);
00839 NotifyPortRename(port);
00840 return 0;
00841 }
00842
00843 }
00844