Correct jackdmp.cpp (failures case were not correct..). Improve JackCoreAudioDriver...
[jack2.git] / common / JackEngine.cpp
blob046e836f08b6351667a7776d584c42a020e75b44
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 General Public License as published by
6 the Free Software Foundation; either version 2 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <iostream>
21 #include <fstream>
22 #include <assert.h>
24 #include "JackSystemDeps.h"
25 #include "JackLockedEngine.h"
26 #include "JackExternalClient.h"
27 #include "JackInternalClient.h"
28 #include "JackEngineControl.h"
29 #include "JackClientControl.h"
30 #include "JackServerGlobals.h"
31 #include "JackGlobals.h"
32 #include "JackChannel.h"
33 #include "JackError.h"
35 namespace Jack
38 #define AssertRefnum(ref) assert(ref >= 0 && ref < CLIENT_NUM);
40 JackEngine::JackEngine(JackGraphManager* manager,
41 JackSynchro* table,
42 JackEngineControl* control)
44 fGraphManager = manager;
45 fSynchroTable = table;
46 fEngineControl = control;
47 for (int i = 0; i < CLIENT_NUM; i++)
48 fClientTable[i] = NULL;
51 JackEngine::~JackEngine()
53 jack_log("JackEngine::~JackEngine");
56 int JackEngine::Open()
58 jack_log("JackEngine::Open");
60 // Open audio thread => request thread communication channel
61 if (fChannel.Open(fEngineControl->fServerName) < 0) {
62 jack_error("Cannot connect to server");
63 return -1;
64 } else {
65 return 0;
69 int JackEngine::Close()
71 jack_log("JackEngine::Close");
72 fChannel.Close();
74 // Close remaining clients (RT is stopped)
75 for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
76 if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
77 jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
78 loadable_client->Close();
79 // Close does not delete the pointer for internal clients
80 fClientTable[i] = NULL;
81 delete loadable_client;
82 } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
83 jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
84 external_client->Close();
85 // Close deletes the pointer for external clients
86 fClientTable[i] = NULL;
90 return 0;
93 //-----------------------------
94 // Client ressource management
95 //-----------------------------
97 int JackEngine::AllocateRefnum()
99 for (int i = 0; i < CLIENT_NUM; i++) {
100 if (!fClientTable[i]) {
101 jack_log("JackEngine::AllocateRefNum ref = %ld", i);
102 return i;
105 return -1;
108 void JackEngine::ReleaseRefnum(int ref)
110 fClientTable[ref] = NULL;
112 if (fEngineControl->fTemporary) {
113 int i;
114 for (i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
115 if (fClientTable[i])
116 break;
118 if (i == CLIENT_NUM) {
119 // last client and temporay case: quit the server
120 jack_log("JackEngine::ReleaseRefnum server quit");
121 fEngineControl->fTemporary = false;
122 JackServerGlobals::fKilled = true;
127 //------------------
128 // Graph management
129 //------------------
131 void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
133 fLastSwitchUsecs = cur_cycle_begin;
134 if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
135 fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
136 fSignal.Signal(); // Signal for threads waiting for next cycle
139 void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
141 if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
142 CheckXRun(cur_cycle_begin);
143 fGraphManager->RunCurrentGraph();
146 bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
148 bool res = true;
150 // Cycle begin
151 fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
153 // Graph
154 if (fGraphManager->IsFinishedGraph()) {
155 ProcessNext(cur_cycle_begin);
156 res = true;
157 } else {
158 jack_log("Process: graph not finished!");
159 if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
160 jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
161 ProcessNext(cur_cycle_begin);
162 res = true;
163 } else {
164 jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
165 ProcessCurrent(cur_cycle_begin);
166 res = false;
170 // Cycle end
171 fEngineControl->CycleEnd(fClientTable);
172 return res;
176 Client that finish *after* the callback date are considered late even if their output buffers may have been
177 correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
180 void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
182 for (int i = fEngineControl->fDriverNum; i < CLIENT_NUM; i++) {
183 JackClientInterface* client = fClientTable[i];
184 if (client && client->GetClientControl()->fActive) {
185 JackClientTiming* timing = fGraphManager->GetClientTiming(i);
186 jack_client_state_t status = timing->fStatus;
187 jack_time_t finished_date = timing->fFinishedAt;
189 if (status != NotTriggered && status != Finished) {
190 jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
191 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
194 if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
195 jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
196 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
202 //---------------
203 // Notifications
204 //---------------
206 void JackEngine::NotifyClient(int refnum, int event, int sync, const char* message, int value1, int value2)
208 JackClientInterface* client = fClientTable[refnum];
210 // The client may be notified by the RT thread while closing
211 if (!client) {
212 jack_log("JackEngine::NotifyClient: client not available anymore");
213 } else if (client->GetClientControl()->fCallback[event]) {
214 if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0)
215 jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
216 } else {
217 jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
221 void JackEngine::NotifyClients(int event, int sync, const char* message, int value1, int value2)
223 for (int i = 0; i < CLIENT_NUM; i++) {
224 JackClientInterface* client = fClientTable[i];
225 if (client) {
226 if (client->GetClientControl()->fCallback[event]) {
227 if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, message, value1, value2) < 0)
228 jack_error("NotifyClient fails name = %s event = %ld val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
229 } else {
230 jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
236 int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
238 jack_log("JackEngine::NotifyAddClient: name = %s", name);
239 // Notify existing clients of the new client and new client of existing clients.
240 for (int i = 0; i < CLIENT_NUM; i++) {
241 JackClientInterface* old_client = fClientTable[i];
242 if (old_client) {
243 if (old_client->ClientNotify(refnum, name, kAddClient, true, "", 0, 0) < 0) {
244 jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
245 return -1;
247 if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, "", 0, 0) < 0) {
248 jack_error("NotifyAddClient new_client fails name = %s", name);
249 return -1;
254 return 0;
257 void JackEngine::NotifyRemoveClient(const char* name, int refnum)
259 // Notify existing clients (including the one beeing suppressed) of the removed client
260 for (int i = 0; i < CLIENT_NUM; i++) {
261 JackClientInterface* client = fClientTable[i];
262 if (client) {
263 client->ClientNotify(refnum, name, kRemoveClient, true, "",0, 0);
268 // Coming from the driver
269 void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
271 // Use the audio thread => request thread communication channel
272 fEngineControl->ResetFrameTime(callback_usecs);
273 fEngineControl->NotifyXRun(delayed_usecs);
274 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
277 void JackEngine::NotifyXRun(int refnum)
279 if (refnum == ALL_CLIENTS) {
280 NotifyClients(kXRunCallback, false, "", 0, 0);
281 } else {
282 NotifyClient(refnum, kXRunCallback, false, "", 0, 0);
286 void JackEngine::NotifyGraphReorder()
288 NotifyClients(kGraphOrderCallback, false, "", 0, 0);
291 void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size)
293 NotifyClients(kBufferSizeCallback, true, "", buffer_size, 0);
296 void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
298 NotifyClients(kSampleRateCallback, true, "", sample_rate, 0);
301 void JackEngine::NotifyFailure(int code, const char* reason)
303 NotifyClients(kShutDownCallback, false, reason, code, 0);
306 void JackEngine::NotifyFreewheel(bool onoff)
308 fEngineControl->fRealTime = !onoff;
309 NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, "", 0, 0);
312 void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
314 NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, "", port_index, 0);
317 void JackEngine::NotifyPortRename(jack_port_id_t port)
319 NotifyClients(kPortRenameCallback, false, "", port, 0);
322 void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
324 NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, "", src, dst);
327 void JackEngine::NotifyActivate(int refnum)
329 NotifyClient(refnum, kActivateClient, true, "", 0, 0);
332 //----------------------------
333 // Loadable client management
334 //----------------------------
336 int JackEngine::GetInternalClientName(int refnum, char* name_res)
338 AssertRefnum(refnum);
339 JackClientInterface* client = fClientTable[refnum];
340 if (client) {
341 strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
342 return 0;
343 } else {
344 return -1;
348 int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
350 // Clear status
351 *status = 0;
353 for (int i = 0; i < CLIENT_NUM; i++) {
354 JackClientInterface* client = fClientTable[i];
355 if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
356 jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
357 *int_ref = i;
358 return 0;
362 *status |= (JackNoSuchClient | JackFailure);
363 return -1;
366 int JackEngine::InternalClientUnload(int refnum, int* status)
368 AssertRefnum(refnum);
369 JackClientInterface* client = fClientTable[refnum];
370 if (client) {
371 int res = client->Close();
372 delete client;
373 *status = 0;
374 return res;
375 } else {
376 *status = (JackNoSuchClient | JackFailure);
377 return -1;
381 //-------------------
382 // Client management
383 //-------------------
385 int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
387 // Clear status
388 *status = 0;
389 strcpy(name_res, name);
391 jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
393 if (protocol != JACK_PROTOCOL_VERSION) {
394 *status |= (JackFailure | JackVersionError);
395 jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
396 return -1;
399 if (ClientCheckName(name)) {
401 *status |= JackNameNotUnique;
403 if (options & JackUseExactName) {
404 jack_error("cannot create new client; %s already exists", name);
405 *status |= JackFailure;
406 return -1;
409 if (GenerateUniqueName(name_res)) {
410 *status |= JackFailure;
411 return -1;
415 return 0;
418 bool JackEngine::GenerateUniqueName(char* name)
420 int tens, ones;
421 int length = strlen(name);
423 if (length > JACK_CLIENT_NAME_SIZE - 4) {
424 jack_error("%s exists and is too long to make unique", name);
425 return true; /* failure */
428 /* generate a unique name by appending "-01".."-99" */
429 name[length++] = '-';
430 tens = length++;
431 ones = length++;
432 name[tens] = '0';
433 name[ones] = '1';
434 name[length] = '\0';
436 while (ClientCheckName(name)) {
437 if (name[ones] == '9') {
438 if (name[tens] == '9') {
439 jack_error("client %s has 99 extra instances already", name);
440 return true; /* give up */
442 name[tens]++;
443 name[ones] = '0';
444 } else {
445 name[ones]++;
448 return false;
451 bool JackEngine::ClientCheckName(const char* name)
453 for (int i = 0; i < CLIENT_NUM; i++) {
454 JackClientInterface* client = fClientTable[i];
455 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
456 return true;
459 return false;
462 int JackEngine::GetClientPID(const char* name)
464 for (int i = 0; i < CLIENT_NUM; i++) {
465 JackClientInterface* client = fClientTable[i];
466 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
467 return client->GetClientControl()->fPID;
470 return 0;
473 int JackEngine::GetClientRefNum(const char* name)
475 for (int i = 0; i < CLIENT_NUM; i++) {
476 JackClientInterface* client = fClientTable[i];
477 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
478 return client->GetClientControl()->fRefNum;
481 return -1;
484 // Used for external clients
485 int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
487 jack_log("JackEngine::ClientOpen: name = %s ", name);
489 int refnum = AllocateRefnum();
490 if (refnum < 0) {
491 jack_error("No more refnum available");
492 return -1;
495 JackExternalClient* client = new JackExternalClient();
497 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
498 jack_error("Cannot allocate synchro");
499 goto error;
502 if (client->Open(name, pid, refnum, shared_client) < 0) {
503 jack_error("Cannot open client");
504 goto error;
507 if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
508 // Failure if RT thread is not running (problem with the driver...)
509 jack_error("Driver is not running");
510 goto error;
513 fClientTable[refnum] = client;
515 if (NotifyAddClient(client, name, refnum) < 0) {
516 jack_error("Cannot notify add client");
517 goto error;
520 fGraphManager->InitRefNum(refnum);
521 fEngineControl->ResetRollingUsecs();
522 *shared_engine = fEngineControl->GetShmIndex();
523 *shared_graph_manager = fGraphManager->GetShmIndex();
524 *ref = refnum;
525 return 0;
527 error:
528 // Cleanup...
529 fSynchroTable[refnum].Destroy();
530 fClientTable[refnum] = 0;
531 client->Close();
532 delete client;
533 return -1;
536 // Used for server driver clients
537 int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
539 jack_log("JackEngine::ClientInternalNew: name = %s", name);
541 int refnum = AllocateRefnum();
542 if (refnum < 0) {
543 jack_error("No more refnum available");
544 goto error;
547 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
548 jack_error("Cannot allocate synchro");
549 goto error;
552 if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
553 // Failure if RT thread is not running (problem with the driver...)
554 jack_error("Driver is not running");
555 goto error;
558 fClientTable[refnum] = client;
560 if (NotifyAddClient(client, name, refnum) < 0) {
561 jack_error("Cannot notify add client");
562 goto error;
565 fGraphManager->InitRefNum(refnum);
566 fEngineControl->ResetRollingUsecs();
567 *shared_engine = fEngineControl;
568 *shared_manager = fGraphManager;
569 *ref = refnum;
570 return 0;
572 error:
573 // Cleanup...
574 fSynchroTable[refnum].Destroy();
575 fClientTable[refnum] = 0;
576 return -1;
579 // Used for external clients
580 int JackEngine::ClientExternalClose(int refnum)
582 AssertRefnum(refnum);
583 JackClientInterface* client = fClientTable[refnum];
585 if (client) {
586 fEngineControl->fTransport.ResetTimebase(refnum);
587 int res = ClientCloseAux(refnum, client, true);
588 client->Close();
589 delete client;
590 return res;
591 } else {
592 return -1;
596 // Used for server internal clients or drivers when the RT thread is stopped
597 int JackEngine::ClientInternalClose(int refnum, bool wait)
599 AssertRefnum(refnum);
600 JackClientInterface* client = fClientTable[refnum];
601 return (client) ? ClientCloseAux(refnum, client, wait) : -1;
604 int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
606 jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
608 // Unregister all ports ==> notifications are sent
609 jack_int_t ports[PORT_NUM_FOR_CLIENT];
610 int i;
612 fGraphManager->GetInputPorts(refnum, ports);
613 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
614 PortUnRegister(refnum, ports[i]);
617 fGraphManager->GetOutputPorts(refnum, ports);
618 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
619 PortUnRegister(refnum, ports[i]);
622 // Remove the client from the table
623 ReleaseRefnum(refnum);
625 // Remove all ports
626 fGraphManager->RemoveAllPorts(refnum);
628 // Wait until next cycle to be sure client is not used anymore
629 if (wait) {
630 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
631 jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
635 // Notify running clients
636 NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
638 // Cleanup...
639 fSynchroTable[refnum].Destroy();
640 fEngineControl->ResetRollingUsecs();
641 return 0;
644 int JackEngine::ClientActivate(int refnum, bool is_real_time)
646 AssertRefnum(refnum);
647 JackClientInterface* client = fClientTable[refnum];
648 assert(fClientTable[refnum]);
650 jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
651 if (is_real_time)
652 fGraphManager->Activate(refnum);
654 // Wait for graph state change to be effective
655 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
656 jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
657 return -1;
658 } else {
659 NotifyActivate(refnum);
660 return 0;
664 // May be called without client
665 int JackEngine::ClientDeactivate(int refnum)
667 AssertRefnum(refnum);
668 JackClientInterface* client = fClientTable[refnum];
669 if (client == NULL)
670 return -1;
672 jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
674 // Disconnect all ports ==> notifications are sent
675 jack_int_t ports[PORT_NUM_FOR_CLIENT];
676 int i;
678 fGraphManager->GetInputPorts(refnum, ports);
679 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
680 PortDisconnect(refnum, ports[i], ALL_PORTS);
683 fGraphManager->GetOutputPorts(refnum, ports);
684 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
685 PortDisconnect(refnum, ports[i], ALL_PORTS);
688 fGraphManager->Deactivate(refnum);
689 fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
691 // Wait for graph state change to be effective
692 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
693 jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
694 return -1;
695 } else {
696 return 0;
700 //-----------------
701 // Port management
702 //-----------------
704 int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
706 jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
707 AssertRefnum(refnum);
708 assert(fClientTable[refnum]);
710 // Check if port name already exists
711 if (fGraphManager->GetPort(name) != NO_PORT) {
712 jack_error("port_name \"%s\" already exists", name);
713 return -1;
716 *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
717 if (*port_index != NO_PORT) {
718 NotifyPortRegistation(*port_index, true);
719 return 0;
720 } else {
721 return -1;
725 int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
727 jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
728 AssertRefnum(refnum);
729 assert(fClientTable[refnum]);
731 // Disconnect port ==> notification is sent
732 PortDisconnect(refnum, port_index, ALL_PORTS);
734 if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
735 NotifyPortRegistation(port_index, false);
736 return 0;
737 } else {
738 return -1;
742 int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
744 jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
745 AssertRefnum(refnum);
746 jack_port_id_t port_src, port_dst;
748 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
749 ? -1
750 : PortConnect(refnum, port_src, port_dst);
753 int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
755 jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
756 AssertRefnum(refnum);
757 JackClientInterface* client;
758 int ref;
760 if (fGraphManager->CheckPorts(src, dst) < 0)
761 return -1;
763 ref = fGraphManager->GetOutputRefNum(src);
764 assert(ref >= 0);
765 client = fClientTable[ref];
766 assert(client);
767 if (!client->GetClientControl()->fActive) {
768 jack_error("Cannot connect ports owned by inactive clients:"
769 " \"%s\" is not active", client->GetClientControl()->fName);
770 return -1;
773 ref = fGraphManager->GetInputRefNum(dst);
774 assert(ref >= 0);
775 client = fClientTable[ref];
776 assert(client);
777 if (!client->GetClientControl()->fActive) {
778 jack_error("Cannot connect ports owned by inactive clients:"
779 " \"%s\" is not active", client->GetClientControl()->fName);
780 return -1;
783 int res = fGraphManager->Connect(src, dst);
784 if (res == 0)
785 NotifyPortConnect(src, dst, true);
786 return res;
789 int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
791 jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
792 AssertRefnum(refnum);
793 jack_port_id_t port_src, port_dst;
795 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
796 ? -1
797 : PortDisconnect(refnum, port_src, port_dst);
800 int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
802 jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
803 AssertRefnum(refnum);
805 if (dst == ALL_PORTS) {
807 jack_int_t connections[CONNECTION_NUM_FOR_PORT];
808 fGraphManager->GetConnections(src, connections);
810 JackPort* port = fGraphManager->GetPort(src);
811 int ret = 0;
812 if (port->GetFlags() & JackPortIsOutput) {
813 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
814 if (PortDisconnect(refnum, src, connections[i]) != 0) {
815 ret = -1;
818 } else {
819 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
820 if (PortDisconnect(refnum, connections[i], src) != 0) {
821 ret = -1;
826 return ret;
827 } else if (fGraphManager->CheckPorts(src, dst) < 0) {
828 return -1;
829 } else if (fGraphManager->Disconnect(src, dst) == 0) {
830 // Notifications
831 NotifyPortConnect(src, dst, false);
832 return 0;
833 } else {
834 return -1;
838 int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
840 AssertRefnum(refnum);
841 fGraphManager->GetPort(port)->SetName(name);
842 NotifyPortRename(port);
843 return 0;
846 } // end of namespace