Simplify server temporary mode : now use a JackTemporaryException.
[jack2.git] / common / JackEngine.cpp
blob537c41607cdf669e234910d48b7587fa70717b96
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 throw JackTemporaryException();
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 if (onoff) {
309 // Save RT state
310 fEngineControl->fSavedRealTime = fEngineControl->fRealTime;
311 fEngineControl->fRealTime = false;
312 } else {
313 // Restore RT state
314 fEngineControl->fRealTime = fEngineControl->fSavedRealTime;
315 fEngineControl->fSavedRealTime = false;
317 NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, "", 0, 0);
320 void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
322 NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, "", port_index, 0);
325 void JackEngine::NotifyPortRename(jack_port_id_t port)
327 NotifyClients(kPortRenameCallback, false, "", port, 0);
330 void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
332 NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, "", src, dst);
335 void JackEngine::NotifyActivate(int refnum)
337 NotifyClient(refnum, kActivateClient, true, "", 0, 0);
340 //----------------------------
341 // Loadable client management
342 //----------------------------
344 int JackEngine::GetInternalClientName(int refnum, char* name_res)
346 AssertRefnum(refnum);
347 JackClientInterface* client = fClientTable[refnum];
348 if (client) {
349 strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
350 return 0;
351 } else {
352 return -1;
356 int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
358 // Clear status
359 *status = 0;
361 for (int i = 0; i < CLIENT_NUM; i++) {
362 JackClientInterface* client = fClientTable[i];
363 if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
364 jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
365 *int_ref = i;
366 return 0;
370 *status |= (JackNoSuchClient | JackFailure);
371 return -1;
374 int JackEngine::InternalClientUnload(int refnum, int* status)
376 AssertRefnum(refnum);
377 JackClientInterface* client = fClientTable[refnum];
378 if (client) {
379 int res = client->Close();
380 delete client;
381 *status = 0;
382 return res;
383 } else {
384 *status = (JackNoSuchClient | JackFailure);
385 return -1;
389 //-------------------
390 // Client management
391 //-------------------
393 int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
395 // Clear status
396 *status = 0;
397 strcpy(name_res, name);
399 jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
401 if (protocol != JACK_PROTOCOL_VERSION) {
402 *status |= (JackFailure | JackVersionError);
403 jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
404 return -1;
407 if (ClientCheckName(name)) {
409 *status |= JackNameNotUnique;
411 if (options & JackUseExactName) {
412 jack_error("cannot create new client; %s already exists", name);
413 *status |= JackFailure;
414 return -1;
417 if (GenerateUniqueName(name_res)) {
418 *status |= JackFailure;
419 return -1;
423 return 0;
426 bool JackEngine::GenerateUniqueName(char* name)
428 int tens, ones;
429 int length = strlen(name);
431 if (length > JACK_CLIENT_NAME_SIZE - 4) {
432 jack_error("%s exists and is too long to make unique", name);
433 return true; /* failure */
436 /* generate a unique name by appending "-01".."-99" */
437 name[length++] = '-';
438 tens = length++;
439 ones = length++;
440 name[tens] = '0';
441 name[ones] = '1';
442 name[length] = '\0';
444 while (ClientCheckName(name)) {
445 if (name[ones] == '9') {
446 if (name[tens] == '9') {
447 jack_error("client %s has 99 extra instances already", name);
448 return true; /* give up */
450 name[tens]++;
451 name[ones] = '0';
452 } else {
453 name[ones]++;
456 return false;
459 bool JackEngine::ClientCheckName(const char* name)
461 for (int i = 0; i < CLIENT_NUM; i++) {
462 JackClientInterface* client = fClientTable[i];
463 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
464 return true;
467 return false;
470 int JackEngine::GetClientPID(const char* name)
472 for (int i = 0; i < CLIENT_NUM; i++) {
473 JackClientInterface* client = fClientTable[i];
474 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
475 return client->GetClientControl()->fPID;
478 return 0;
481 int JackEngine::GetClientRefNum(const char* name)
483 for (int i = 0; i < CLIENT_NUM; i++) {
484 JackClientInterface* client = fClientTable[i];
485 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
486 return client->GetClientControl()->fRefNum;
489 return -1;
492 // Used for external clients
493 int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
495 jack_log("JackEngine::ClientExternalOpen: name = %s ", name);
497 int refnum = AllocateRefnum();
498 if (refnum < 0) {
499 jack_error("No more refnum available");
500 return -1;
503 JackExternalClient* client = new JackExternalClient();
505 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
506 jack_error("Cannot allocate synchro");
507 goto error;
510 if (client->Open(name, pid, refnum, shared_client) < 0) {
511 jack_error("Cannot open client");
512 goto error;
515 if (!fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
516 // Failure if RT thread is not running (problem with the driver...)
517 jack_error("Driver is not running");
518 goto error;
521 fClientTable[refnum] = client;
523 if (NotifyAddClient(client, name, refnum) < 0) {
524 jack_error("Cannot notify add client");
525 goto error;
528 fGraphManager->InitRefNum(refnum);
529 fEngineControl->ResetRollingUsecs();
530 *shared_engine = fEngineControl->GetShmIndex();
531 *shared_graph_manager = fGraphManager->GetShmIndex();
532 *ref = refnum;
533 return 0;
535 error:
536 // Cleanup...
537 fSynchroTable[refnum].Destroy();
538 fClientTable[refnum] = 0;
539 client->Close();
540 delete client;
541 return -1;
544 // Used for server driver clients
545 int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
547 jack_log("JackEngine::ClientInternalOpen: name = %s", name);
549 int refnum = AllocateRefnum();
550 if (refnum < 0) {
551 jack_error("No more refnum available");
552 goto error;
555 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
556 jack_error("Cannot allocate synchro");
557 goto error;
560 if (wait && !fSignal.LockedTimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
561 // Failure if RT thread is not running (problem with the driver...)
562 jack_error("Driver is not running");
563 goto error;
566 fClientTable[refnum] = client;
568 if (NotifyAddClient(client, name, refnum) < 0) {
569 jack_error("Cannot notify add client");
570 goto error;
573 fGraphManager->InitRefNum(refnum);
574 fEngineControl->ResetRollingUsecs();
575 *shared_engine = fEngineControl;
576 *shared_manager = fGraphManager;
577 *ref = refnum;
578 return 0;
580 error:
581 // Cleanup...
582 fSynchroTable[refnum].Destroy();
583 fClientTable[refnum] = 0;
584 return -1;
587 // Used for external clients
588 int JackEngine::ClientExternalClose(int refnum)
590 AssertRefnum(refnum);
591 JackClientInterface* client = fClientTable[refnum];
593 if (client) {
594 fEngineControl->fTransport.ResetTimebase(refnum);
595 int res = ClientCloseAux(refnum, client, true);
596 client->Close();
597 delete client;
598 return res;
599 } else {
600 return -1;
604 // Used for server internal clients or drivers when the RT thread is stopped
605 int JackEngine::ClientInternalClose(int refnum, bool wait)
607 AssertRefnum(refnum);
608 JackClientInterface* client = fClientTable[refnum];
609 return (client) ? ClientCloseAux(refnum, client, wait) : -1;
612 int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
614 jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
616 // Unregister all ports ==> notifications are sent
617 jack_int_t ports[PORT_NUM_FOR_CLIENT];
618 int i;
620 fGraphManager->GetInputPorts(refnum, ports);
621 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
622 PortUnRegister(refnum, ports[i]);
625 fGraphManager->GetOutputPorts(refnum, ports);
626 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
627 PortUnRegister(refnum, ports[i]);
630 // Remove the client from the table
631 ReleaseRefnum(refnum);
633 // Remove all ports
634 fGraphManager->RemoveAllPorts(refnum);
636 // Wait until next cycle to be sure client is not used anymore
637 if (wait) {
638 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
639 jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
643 // Notify running clients
644 NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
646 // Cleanup...
647 fSynchroTable[refnum].Destroy();
648 fEngineControl->ResetRollingUsecs();
649 return 0;
652 int JackEngine::ClientActivate(int refnum, bool is_real_time)
654 AssertRefnum(refnum);
655 JackClientInterface* client = fClientTable[refnum];
656 assert(fClientTable[refnum]);
658 jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
659 if (is_real_time)
660 fGraphManager->Activate(refnum);
662 // Wait for graph state change to be effective
663 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
664 jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
665 return -1;
666 } else {
667 NotifyActivate(refnum);
668 return 0;
672 // May be called without client
673 int JackEngine::ClientDeactivate(int refnum)
675 AssertRefnum(refnum);
676 JackClientInterface* client = fClientTable[refnum];
677 if (client == NULL)
678 return -1;
680 jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
682 // Disconnect all ports ==> notifications are sent
683 jack_int_t ports[PORT_NUM_FOR_CLIENT];
684 int i;
686 fGraphManager->GetInputPorts(refnum, ports);
687 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
688 PortDisconnect(refnum, ports[i], ALL_PORTS);
691 fGraphManager->GetOutputPorts(refnum, ports);
692 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
693 PortDisconnect(refnum, ports[i], ALL_PORTS);
696 fGraphManager->Deactivate(refnum);
697 fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
699 // Wait for graph state change to be effective
700 if (!fSignal.LockedTimedWait(fEngineControl->fTimeOutUsecs * 10)) {
701 jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
702 return -1;
703 } else {
704 return 0;
708 //-----------------
709 // Port management
710 //-----------------
712 int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
714 jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
715 AssertRefnum(refnum);
716 assert(fClientTable[refnum]);
718 // Check if port name already exists
719 if (fGraphManager->GetPort(name) != NO_PORT) {
720 jack_error("port_name \"%s\" already exists", name);
721 return -1;
724 *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
725 if (*port_index != NO_PORT) {
726 NotifyPortRegistation(*port_index, true);
727 return 0;
728 } else {
729 return -1;
733 int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
735 jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
736 AssertRefnum(refnum);
737 assert(fClientTable[refnum]);
739 // Disconnect port ==> notification is sent
740 PortDisconnect(refnum, port_index, ALL_PORTS);
742 if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
743 NotifyPortRegistation(port_index, false);
744 return 0;
745 } else {
746 return -1;
750 int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
752 jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
753 AssertRefnum(refnum);
754 jack_port_id_t port_src, port_dst;
756 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
757 ? -1
758 : PortConnect(refnum, port_src, port_dst);
761 int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
763 jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
764 AssertRefnum(refnum);
765 JackClientInterface* client;
766 int ref;
768 if (fGraphManager->CheckPorts(src, dst) < 0)
769 return -1;
771 ref = fGraphManager->GetOutputRefNum(src);
772 assert(ref >= 0);
773 client = fClientTable[ref];
774 assert(client);
775 if (!client->GetClientControl()->fActive) {
776 jack_error("Cannot connect ports owned by inactive clients:"
777 " \"%s\" is not active", client->GetClientControl()->fName);
778 return -1;
781 ref = fGraphManager->GetInputRefNum(dst);
782 assert(ref >= 0);
783 client = fClientTable[ref];
784 assert(client);
785 if (!client->GetClientControl()->fActive) {
786 jack_error("Cannot connect ports owned by inactive clients:"
787 " \"%s\" is not active", client->GetClientControl()->fName);
788 return -1;
791 int res = fGraphManager->Connect(src, dst);
792 if (res == 0)
793 NotifyPortConnect(src, dst, true);
794 return res;
797 int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
799 jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
800 AssertRefnum(refnum);
801 jack_port_id_t port_src, port_dst;
803 return (fGraphManager->GetTwoPorts(src, dst, &port_src, &port_dst) < 0)
804 ? -1
805 : PortDisconnect(refnum, port_src, port_dst);
808 int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
810 jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
811 AssertRefnum(refnum);
813 if (dst == ALL_PORTS) {
815 jack_int_t connections[CONNECTION_NUM_FOR_PORT];
816 fGraphManager->GetConnections(src, connections);
818 JackPort* port = fGraphManager->GetPort(src);
819 int ret = 0;
820 if (port->GetFlags() & JackPortIsOutput) {
821 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
822 if (PortDisconnect(refnum, src, connections[i]) != 0) {
823 ret = -1;
826 } else {
827 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
828 if (PortDisconnect(refnum, connections[i], src) != 0) {
829 ret = -1;
834 return ret;
835 } else if (fGraphManager->CheckPorts(src, dst) < 0) {
836 return -1;
837 } else if (fGraphManager->Disconnect(src, dst) == 0) {
838 // Notifications
839 NotifyPortConnect(src, dst, false);
840 return 0;
841 } else {
842 return -1;
846 int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
848 AssertRefnum(refnum);
849 fGraphManager->GetPort(port)->SetName(name);
850 NotifyPortRename(port);
851 return 0;
854 } // end of namespace