Better isolation of server and clients system resources to allow starting the server...
[jack2.git] / common / JackEngine.cpp
blob80a30aa9dd6bdf000d8a35dccea9a6d189fc840f
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 "JackGlobals.h"
31 #include "JackChannel.h"
32 #include "JackError.h"
34 namespace Jack
37 #define AssertRefnum(ref) assert(ref >= 0 && ref < CLIENT_NUM);
39 JackEngine::JackEngine(JackGraphManager* manager,
40 JackSynchro* table,
41 JackEngineControl* control)
43 fGraphManager = manager;
44 fSynchroTable = table;
45 fEngineControl = control;
46 for (int i = 0; i < CLIENT_NUM; i++)
47 fClientTable[i] = NULL;
50 JackEngine::~JackEngine()
52 jack_log("JackEngine::~JackEngine");
55 int JackEngine::Open()
57 jack_log("JackEngine::Open");
59 // Open audio thread => request thread communication channel
60 if (fChannel.Open(fEngineControl->fServerName) < 0) {
61 jack_error("Cannot connect to server");
62 return -1;
63 } else {
64 return 0;
68 int JackEngine::Close()
70 jack_log("JackEngine::Close");
71 fChannel.Close();
73 // Close remaining clients (RT is stopped)
74 for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
75 if (JackLoadableInternalClient* loadable_client = dynamic_cast<JackLoadableInternalClient*>(fClientTable[i])) {
76 jack_log("JackEngine::Close loadable client = %s", loadable_client->GetClientControl()->fName);
77 loadable_client->Close();
78 // Close does not delete the pointer for internal clients
79 fClientTable[i] = NULL;
80 delete loadable_client;
81 } else if (JackExternalClient* external_client = dynamic_cast<JackExternalClient*>(fClientTable[i])) {
82 jack_log("JackEngine::Close external client = %s", external_client->GetClientControl()->fName);
83 external_client->Close();
84 // Close deletes the pointer for external clients
85 fClientTable[i] = NULL;
89 fSignal.Destroy();
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 = REAL_REFNUM; 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 #ifndef WIN32
123 exit(0);
124 #endif
129 //------------------
130 // Graph management
131 //------------------
133 void JackEngine::ProcessNext(jack_time_t cur_cycle_begin)
135 fLastSwitchUsecs = cur_cycle_begin;
136 if (fGraphManager->RunNextGraph()) // True if the graph actually switched to a new state
137 fChannel.Notify(ALL_CLIENTS, kGraphOrderCallback, 0);
138 fSignal.SignalAll(); // Signal for threads waiting for next cycle
141 void JackEngine::ProcessCurrent(jack_time_t cur_cycle_begin)
143 if (cur_cycle_begin < fLastSwitchUsecs + 2 * fEngineControl->fPeriodUsecs) // Signal XRun only for the first failing cycle
144 CheckXRun(cur_cycle_begin);
145 fGraphManager->RunCurrentGraph();
148 bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end)
150 bool res = true;
152 // Cycle begin
153 fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
155 // Graph
156 if (fGraphManager->IsFinishedGraph()) {
157 ProcessNext(cur_cycle_begin);
158 res = true;
159 } else {
160 jack_log("Process: graph not finished!");
161 if (cur_cycle_begin > fLastSwitchUsecs + fEngineControl->fTimeOutUsecs) {
162 jack_log("Process: switch to next state delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
163 ProcessNext(cur_cycle_begin);
164 res = true;
165 } else {
166 jack_log("Process: waiting to switch delta = %ld", long(cur_cycle_begin - fLastSwitchUsecs));
167 ProcessCurrent(cur_cycle_begin);
168 res = false;
172 // Cycle end
173 fEngineControl->CycleEnd(fClientTable);
174 return res;
179 Client that finish *after* the callback date are considered late even if their output buffers may have been
180 correctly mixed in the time window: callbackUsecs <==> Read <==> Write.
183 void JackEngine::CheckXRun(jack_time_t callback_usecs) // REVOIR les conditions de fin
185 for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
186 JackClientInterface* client = fClientTable[i];
187 if (client && client->GetClientControl()->fActive) {
188 JackClientTiming* timing = fGraphManager->GetClientTiming(i);
189 jack_client_state_t status = timing->fStatus;
190 jack_time_t finished_date = timing->fFinishedAt;
192 if (status != NotTriggered && status != Finished) {
193 jack_error("JackEngine::XRun: client = %s was not run: state = %ld", client->GetClientControl()->fName, status);
194 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
197 if (status == Finished && (long)(finished_date - callback_usecs) > 0) {
198 jack_error("JackEngine::XRun: client %s finished after current callback", client->GetClientControl()->fName);
199 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0); // Notify all clients
205 //---------------
206 // Notifications
207 //---------------
209 void JackEngine::NotifyClient(int refnum, int event, int sync, int value1, int value2)
211 JackClientInterface* client = fClientTable[refnum];
213 // The client may be notified by the RT thread while closing
214 if (!client) {
215 jack_log("JackEngine::NotifyClient: client not available anymore");
216 } else if (client->GetClientControl()->fCallback[event]) {
217 if (client->ClientNotify(refnum, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
218 jack_error("NotifyClient fails name = %s event = %ld = val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
219 } else {
220 jack_log("JackEngine::NotifyClient: no callback for event = %ld", event);
224 void JackEngine::NotifyClients(int event, int sync, int value1, int value2)
226 for (int i = 0; i < CLIENT_NUM; i++) {
227 JackClientInterface* client = fClientTable[i];
228 if (client) {
229 if (client->GetClientControl()->fCallback[event]) {
230 if (client->ClientNotify(i, client->GetClientControl()->fName, event, sync, value1, value2) < 0)
231 jack_error("NotifyClient fails name = %s event = %ld = val1 = %ld val2 = %ld", client->GetClientControl()->fName, event, value1, value2);
232 } else {
233 jack_log("JackEngine::NotifyClients: no callback for event = %ld", event);
239 int JackEngine::NotifyAddClient(JackClientInterface* new_client, const char* name, int refnum)
241 // Notify existing clients of the new client and new client of existing clients.
242 for (int i = 0; i < CLIENT_NUM; i++) {
243 JackClientInterface* old_client = fClientTable[i];
244 if (old_client) {
245 if (old_client->ClientNotify(refnum, name, kAddClient, true, 0, 0) < 0) {
246 jack_error("NotifyAddClient old_client fails name = %s", old_client->GetClientControl()->fName);
247 return -1;
249 if (new_client->ClientNotify(i, old_client->GetClientControl()->fName, kAddClient, true, 0, 0) < 0) {
250 jack_error("NotifyAddClient new_client fails name = %s", name);
251 return -1;
256 return 0;
259 void JackEngine::NotifyRemoveClient(const char* name, int refnum)
261 // Notify existing clients (including the one beeing suppressed) of the removed client
262 for (int i = 0; i < CLIENT_NUM; i++) {
263 JackClientInterface* client = fClientTable[i];
264 if (client) {
265 client->ClientNotify(refnum, name, kRemoveClient, true, 0, 0);
270 // Coming from the driver
271 void JackEngine::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
273 // Use the audio thread => request thread communication channel
274 fEngineControl->ResetFrameTime(callback_usecs);
275 fEngineControl->NotifyXRun(delayed_usecs);
276 fChannel.Notify(ALL_CLIENTS, kXRunCallback, 0);
279 void JackEngine::NotifyXRun(int refnum)
281 if (refnum == ALL_CLIENTS) {
282 NotifyClients(kXRunCallback, false, 0, 0);
283 } else {
284 NotifyClient(refnum, kXRunCallback, false, 0, 0);
288 void JackEngine::NotifyGraphReorder()
290 NotifyClients(kGraphOrderCallback, false, 0, 0);
293 void JackEngine::NotifyBufferSize(jack_nframes_t buffer_size)
295 NotifyClients(kBufferSizeCallback, true, buffer_size, 0);
298 void JackEngine::NotifySampleRate(jack_nframes_t sample_rate)
300 NotifyClients(kSampleRateCallback, true, sample_rate, 0);
303 void JackEngine::NotifyFreewheel(bool onoff)
305 fEngineControl->fRealTime = !onoff;
306 NotifyClients((onoff ? kStartFreewheelCallback : kStopFreewheelCallback), true, 0, 0);
309 void JackEngine::NotifyPortRegistation(jack_port_id_t port_index, bool onoff)
311 NotifyClients((onoff ? kPortRegistrationOnCallback : kPortRegistrationOffCallback), false, port_index, 0);
314 void JackEngine::NotifyPortRename(jack_port_id_t port)
316 NotifyClients(kPortRenameCallback, false, port, 0);
319 void JackEngine::NotifyPortConnect(jack_port_id_t src, jack_port_id_t dst, bool onoff)
321 NotifyClients((onoff ? kPortConnectCallback : kPortDisconnectCallback), false, src, dst);
324 void JackEngine::NotifyActivate(int refnum)
326 NotifyClient(refnum, kActivateClient, true, 0, 0);
329 //----------------------------
330 // Loadable client management
331 //----------------------------
333 int JackEngine::GetInternalClientName(int refnum, char* name_res)
335 AssertRefnum(refnum);
336 JackClientInterface* client = fClientTable[refnum];
337 if (client) {
338 strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
339 return 0;
340 } else {
341 return -1;
345 int JackEngine::InternalClientHandle(const char* client_name, int* status, int* int_ref)
347 // Clear status
348 *status = 0;
350 for (int i = 0; i < CLIENT_NUM; i++) {
351 JackClientInterface* client = fClientTable[i];
352 if (client && dynamic_cast<JackLoadableInternalClient*>(client) && (strcmp(client->GetClientControl()->fName, client_name) == 0)) {
353 jack_log("InternalClientHandle found client name = %s ref = %ld", client_name, i);
354 *int_ref = i;
355 return 0;
359 *status |= (JackNoSuchClient | JackFailure);
360 return -1;
363 int JackEngine::InternalClientUnload(int refnum, int* status)
365 AssertRefnum(refnum);
366 JackClientInterface* client = fClientTable[refnum];
367 if (client) {
368 int res = client->Close();
369 delete client;
370 *status = 0;
371 return res;
372 } else {
373 *status = (JackNoSuchClient | JackFailure);
374 return -1;
378 //-------------------
379 // Client management
380 //-------------------
382 int JackEngine::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status)
384 // Clear status
385 *status = 0;
386 strcpy(name_res, name);
388 jack_log("Check protocol client %ld server = %ld", protocol, JACK_PROTOCOL_VERSION);
390 if (protocol != JACK_PROTOCOL_VERSION) {
391 *status |= (JackFailure | JackVersionError);
392 jack_error("JACK protocol mismatch (%d vs %d)", protocol, JACK_PROTOCOL_VERSION);
393 return -1;
396 if (ClientCheckName(name)) {
398 *status |= JackNameNotUnique;
400 if (options & JackUseExactName) {
401 jack_error("cannot create new client; %s already exists", name);
402 *status |= JackFailure;
403 return -1;
406 if (GenerateUniqueName(name_res)) {
407 *status |= JackFailure;
408 return -1;
412 return 0;
415 bool JackEngine::GenerateUniqueName(char* name)
417 int tens, ones;
418 int length = strlen(name);
420 if (length > JACK_CLIENT_NAME_SIZE - 4) {
421 jack_error("%s exists and is too long to make unique", name);
422 return true; /* failure */
425 /* generate a unique name by appending "-01".."-99" */
426 name[length++] = '-';
427 tens = length++;
428 ones = length++;
429 name[tens] = '0';
430 name[ones] = '1';
431 name[length] = '\0';
433 while (ClientCheckName(name)) {
434 if (name[ones] == '9') {
435 if (name[tens] == '9') {
436 jack_error("client %s has 99 extra instances already", name);
437 return true; /* give up */
439 name[tens]++;
440 name[ones] = '0';
441 } else {
442 name[ones]++;
445 return false;
448 bool JackEngine::ClientCheckName(const char* name)
450 for (int i = 0; i < CLIENT_NUM; i++) {
451 JackClientInterface* client = fClientTable[i];
452 if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
453 return true;
456 return false;
459 int JackEngine::GetClientPID(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 client->GetClientControl()->fPID;
467 return 0;
470 // Used for external clients
471 int JackEngine::ClientExternalOpen(const char* name, int pid, int* ref, int* shared_engine, int* shared_client, int* shared_graph_manager)
473 jack_log("JackEngine::ClientOpen: name = %s ", name);
475 int refnum = AllocateRefnum();
476 if (refnum < 0) {
477 jack_error("No more refnum available");
478 return -1;
481 JackExternalClient* client = new JackExternalClient();
483 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
484 jack_error("Cannot allocate synchro");
485 goto error;
488 if (client->Open(name, pid, refnum, shared_client) < 0) {
489 jack_error("Cannot open client");
490 goto error;
493 if (!fSignal.TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
494 // Failure if RT thread is not running (problem with the driver...)
495 jack_error("Driver is not running");
496 goto error;
499 fClientTable[refnum] = client;
501 if (NotifyAddClient(client, name, refnum) < 0) {
502 jack_error("Cannot notify add client");
503 goto error;
506 fGraphManager->InitRefNum(refnum);
507 fEngineControl->ResetRollingUsecs();
508 *shared_engine = fEngineControl->GetShmIndex();
509 *shared_graph_manager = fGraphManager->GetShmIndex();
510 *ref = refnum;
511 return 0;
513 error:
514 // Cleanup...
515 fSynchroTable[refnum].Destroy();
516 fClientTable[refnum] = 0;
517 client->Close();
518 delete client;
519 return -1;
522 // Used for server driver clients
523 int JackEngine::ClientInternalOpen(const char* name, int* ref, JackEngineControl** shared_engine, JackGraphManager** shared_manager, JackClientInterface* client, bool wait)
525 jack_log("JackEngine::ClientInternalNew: name = %s", name);
527 int refnum = AllocateRefnum();
528 if (refnum < 0) {
529 jack_error("No more refnum available");
530 goto error;
533 if (!fSynchroTable[refnum].Allocate(name, fEngineControl->fServerName, 0)) {
534 jack_error("Cannot allocate synchro");
535 goto error;
538 if (wait && !fSignal.TimedWait(DRIVER_OPEN_TIMEOUT * 1000000)) {
539 // Failure if RT thread is not running (problem with the driver...)
540 jack_error("Driver is not running");
541 goto error;
544 fClientTable[refnum] = client;
546 if (NotifyAddClient(client, name, refnum) < 0) {
547 jack_error("Cannot notify add client");
548 goto error;
551 fGraphManager->InitRefNum(refnum);
552 fEngineControl->ResetRollingUsecs();
553 *shared_engine = fEngineControl;
554 *shared_manager = fGraphManager;
555 *ref = refnum;
556 return 0;
558 error:
559 // Cleanup...
560 fSynchroTable[refnum].Destroy();
561 fClientTable[refnum] = 0;
562 return -1;
565 // Used for external clients
566 int JackEngine::ClientExternalClose(int refnum)
568 AssertRefnum(refnum);
569 JackClientInterface* client = fClientTable[refnum];
571 if (client) {
572 fEngineControl->fTransport.ResetTimebase(refnum);
573 int res = ClientCloseAux(refnum, client, true);
574 client->Close();
575 delete client;
576 return res;
577 } else {
578 return -1;
582 // Used for server internal clients or drivers when the RT thread is stopped
583 int JackEngine::ClientInternalClose(int refnum, bool wait)
585 AssertRefnum(refnum);
586 JackClientInterface* client = fClientTable[refnum];
587 return (client) ? ClientCloseAux(refnum, client, wait) : -1;
590 int JackEngine::ClientCloseAux(int refnum, JackClientInterface* client, bool wait)
592 jack_log("JackEngine::ClientCloseAux ref = %ld", refnum);
594 // Unregister all ports ==> notifications are sent
595 jack_int_t ports[PORT_NUM_FOR_CLIENT];
596 int i;
598 fGraphManager->GetInputPorts(refnum, ports);
599 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
600 PortUnRegister(refnum, ports[i]);
603 fGraphManager->GetOutputPorts(refnum, ports);
604 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
605 PortUnRegister(refnum, ports[i]);
608 // Remove the client from the table
609 ReleaseRefnum(refnum);
611 // Remove all ports
612 fGraphManager->RemoveAllPorts(refnum);
614 // Wait until next cycle to be sure client is not used anymore
615 if (wait) {
616 if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 2)) { // Must wait at least until a switch occurs in Process, even in case of graph end failure
617 jack_error("JackEngine::ClientCloseAux wait error ref = %ld", refnum);
621 // Notify running clients
622 NotifyRemoveClient(client->GetClientControl()->fName, client->GetClientControl()->fRefNum);
624 // Cleanup...
625 fSynchroTable[refnum].Destroy();
626 fEngineControl->ResetRollingUsecs();
627 return 0;
630 int JackEngine::ClientActivate(int refnum, bool state)
632 AssertRefnum(refnum);
633 JackClientInterface* client = fClientTable[refnum];
634 assert(fClientTable[refnum]);
636 jack_log("JackEngine::ClientActivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
637 if (state)
638 fGraphManager->Activate(refnum);
640 // Wait for graph state change to be effective
641 if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
642 jack_error("JackEngine::ClientActivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
643 return -1;
644 } else {
645 NotifyActivate(refnum);
646 return 0;
650 // May be called without client
651 int JackEngine::ClientDeactivate(int refnum)
653 AssertRefnum(refnum);
654 JackClientInterface* client = fClientTable[refnum];
655 if (client == NULL)
656 return -1;
658 jack_log("JackEngine::ClientDeactivate ref = %ld name = %s", refnum, client->GetClientControl()->fName);
660 // Disconnect all ports ==> notifications are sent
661 jack_int_t ports[PORT_NUM_FOR_CLIENT];
662 int i;
664 fGraphManager->GetInputPorts(refnum, ports);
665 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
666 PortDisconnect(refnum, ports[i], ALL_PORTS);
669 fGraphManager->GetOutputPorts(refnum, ports);
670 for (i = 0; (i < PORT_NUM_FOR_CLIENT) && (ports[i] != EMPTY) ; i++) {
671 PortDisconnect(refnum, ports[i], ALL_PORTS);
674 fGraphManager->Deactivate(refnum);
675 fLastSwitchUsecs = 0; // Force switch to occur next cycle, even when called with "dead" clients
677 // Wait for graph state change to be effective
678 if (!fSignal.TimedWait(fEngineControl->fTimeOutUsecs * 10)) {
679 jack_error("JackEngine::ClientDeactivate wait error ref = %ld name = %s", refnum, client->GetClientControl()->fName);
680 return -1;
681 } else {
682 return 0;
686 //-----------------
687 // Port management
688 //-----------------
690 int JackEngine::PortRegister(int refnum, const char* name, const char *type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index)
692 jack_log("JackEngine::PortRegister ref = %ld name = %s type = %s flags = %d buffer_size = %d", refnum, name, type, flags, buffer_size);
693 AssertRefnum(refnum);
694 assert(fClientTable[refnum]);
696 // Check if port name already exists
697 if (fGraphManager->GetPort(name) != NO_PORT) {
698 jack_error("port_name \"%s\" already exists", name);
699 return -1;
702 *port_index = fGraphManager->AllocatePort(refnum, name, type, (JackPortFlags)flags, fEngineControl->fBufferSize);
703 if (*port_index != NO_PORT) {
704 NotifyPortRegistation(*port_index, true);
705 return 0;
706 } else {
707 return -1;
711 int JackEngine::PortUnRegister(int refnum, jack_port_id_t port_index)
713 jack_log("JackEngine::PortUnRegister ref = %ld port_index = %ld", refnum, port_index);
714 AssertRefnum(refnum);
715 assert(fClientTable[refnum]);
717 // Disconnect port ==> notification is sent
718 PortDisconnect(refnum, port_index, ALL_PORTS);
720 if (fGraphManager->ReleasePort(refnum, port_index) == 0) {
721 NotifyPortRegistation(port_index, false);
722 return 0;
723 } else {
724 return -1;
728 int JackEngine::PortConnect(int refnum, const char* src, const char* dst)
730 jack_log("JackEngine::PortConnect src = %s dst = %s", src, dst);
731 jack_port_id_t port_src, port_dst;
733 return (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0)
734 ? -1
735 : PortConnect(refnum, port_src, port_dst);
738 int JackEngine::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
740 jack_log("JackEngine::PortConnect src = %d dst = %d", src, dst);
741 AssertRefnum(refnum);
742 JackClientInterface* client;
743 int ref;
745 if (fGraphManager->CheckPorts(src, dst) < 0)
746 return -1;
748 ref = fGraphManager->GetOutputRefNum(src);
749 assert(ref >= 0);
750 client = fClientTable[ref];
751 assert(client);
752 if (!client->GetClientControl()->fActive) {
753 jack_error("Cannot connect ports owned by inactive clients:"
754 " \"%s\" is not active", client->GetClientControl()->fName);
755 return -1;
758 ref = fGraphManager->GetInputRefNum(dst);
759 assert(ref >= 0);
760 client = fClientTable[ref];
761 assert(client);
762 if (!client->GetClientControl()->fActive) {
763 jack_error("Cannot connect ports owned by inactive clients:"
764 " \"%s\" is not active", client->GetClientControl()->fName);
765 return -1;
768 int res = fGraphManager->Connect(src, dst);
769 if (res == 0)
770 NotifyPortConnect(src, dst, true);
771 return res;
774 int JackEngine::PortDisconnect(int refnum, const char* src, const char* dst)
776 jack_log("JackEngine::PortDisconnect src = %s dst = %s", src, dst);
777 AssertRefnum(refnum);
778 jack_port_id_t port_src, port_dst;
780 if (fGraphManager->CheckPorts(src, dst, &port_src, &port_dst) < 0) {
781 return -1;
782 } else if (fGraphManager->Disconnect(port_src, port_dst) == 0) {
783 NotifyPortConnect(port_src, port_dst, false);
784 return 0;
785 } else {
786 return -1;
790 int JackEngine::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst)
792 jack_log("JackEngine::PortDisconnect src = %d dst = %d", src, dst);
793 AssertRefnum(refnum);
795 if (dst == ALL_PORTS) {
797 jack_int_t connections[CONNECTION_NUM_FOR_PORT];
798 fGraphManager->GetConnections(src, connections);
800 // Notifications
801 JackPort* port = fGraphManager->GetPort(src);
802 if (port->GetFlags() & JackPortIsOutput) {
803 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
804 jack_log("NotifyPortConnect src = %ld dst = %ld false", src, connections[i]);
805 NotifyPortConnect(src, connections[i], false);
807 } else {
808 for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && (connections[i] != EMPTY); i++) {
809 jack_log("NotifyPortConnect src = %ld dst = %ld false", connections[i], src);
810 NotifyPortConnect(connections[i], src, false);
814 return fGraphManager->DisconnectAll(src);
815 } else if (fGraphManager->CheckPorts(src, dst) < 0) {
816 return -1;
817 } else if (fGraphManager->Disconnect(src, dst) == 0) {
818 // Notifications
819 NotifyPortConnect(src, dst, false);
820 return 0;
821 } else {
822 return -1;
826 int JackEngine::PortRename(int refnum, jack_port_id_t port, const char* name)
828 fGraphManager->GetPort(port)->SetName(name);
829 NotifyPortRename(port);
830 return 0;
833 } // end of namespace