jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

904 lines
28KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. 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 Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackGraphManager.h"
  17. #include "JackConstants.h"
  18. #include "JackError.h"
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <algorithm>
  22. #include <regex.h>
  23. namespace Jack
  24. {
  25. static void AssertBufferSize(jack_nframes_t buffer_size)
  26. {
  27. if (buffer_size > BUFFER_SIZE_MAX) {
  28. jack_log("JackGraphManager::AssertBufferSize frames = %ld", buffer_size);
  29. assert(buffer_size <= BUFFER_SIZE_MAX);
  30. }
  31. }
  32. void JackGraphManager::AssertPort(jack_port_id_t port_index)
  33. {
  34. if (port_index >= fPortMax) {
  35. jack_log("JackGraphManager::AssertPort port_index = %ld", port_index);
  36. assert(port_index < fPortMax);
  37. }
  38. }
  39. JackGraphManager* JackGraphManager::Allocate(int port_max)
  40. {
  41. // Using "Placement" new
  42. void* shared_ptr = JackShmMem::operator new(sizeof(JackGraphManager) + port_max * sizeof(JackPort));
  43. return new(shared_ptr) JackGraphManager(port_max);
  44. }
  45. void JackGraphManager::Destroy(JackGraphManager* manager)
  46. {
  47. // "Placement" new was used
  48. manager->~JackGraphManager();
  49. JackShmMem::operator delete(manager);
  50. }
  51. JackGraphManager::JackGraphManager(int port_max)
  52. {
  53. assert(port_max <= PORT_NUM_MAX);
  54. for (int i = 0; i < port_max; i++) {
  55. fPortArray[i].Release();
  56. }
  57. fPortMax = port_max;
  58. }
  59. JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
  60. {
  61. AssertPort(port_index);
  62. return &fPortArray[port_index];
  63. }
  64. float* JackGraphManager::GetBuffer(jack_port_id_t port_index)
  65. {
  66. return fPortArray[port_index].GetBuffer();
  67. }
  68. // Server
  69. void JackGraphManager::InitRefNum(int refnum)
  70. {
  71. JackConnectionManager* manager = WriteNextStateStart();
  72. manager->InitRefNum(refnum);
  73. WriteNextStateStop();
  74. }
  75. // RT
  76. void JackGraphManager::RunCurrentGraph()
  77. {
  78. JackConnectionManager* manager = ReadCurrentState();
  79. manager->ResetGraph(fClientTiming);
  80. }
  81. // RT
  82. bool JackGraphManager::RunNextGraph()
  83. {
  84. bool res;
  85. JackConnectionManager* manager = TrySwitchState(&res);
  86. manager->ResetGraph(fClientTiming);
  87. return res;
  88. }
  89. // RT
  90. bool JackGraphManager::IsFinishedGraph()
  91. {
  92. JackConnectionManager* manager = ReadCurrentState();
  93. return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
  94. }
  95. // RT
  96. int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro* table)
  97. {
  98. JackConnectionManager* manager = ReadCurrentState();
  99. return manager->ResumeRefNum(control, table, fClientTiming);
  100. }
  101. // RT
  102. int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro* table, long usec)
  103. {
  104. JackConnectionManager* manager = ReadCurrentState();
  105. return manager->SuspendRefNum(control, table, fClientTiming, usec);
  106. }
  107. void JackGraphManager::TopologicalSort(std::vector<jack_int_t>& sorted)
  108. {
  109. UInt16 cur_index;
  110. UInt16 next_index;
  111. do {
  112. cur_index = GetCurrentIndex();
  113. sorted.clear();
  114. ReadCurrentState()->TopologicalSort(sorted);
  115. next_index = GetCurrentIndex();
  116. } while (cur_index != next_index); // Until a coherent state has been read
  117. }
  118. // Server
  119. void JackGraphManager::DirectConnect(int ref1, int ref2)
  120. {
  121. JackConnectionManager* manager = WriteNextStateStart();
  122. manager->DirectConnect(ref1, ref2);
  123. jack_log("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  124. WriteNextStateStop();
  125. }
  126. // Server
  127. void JackGraphManager::DirectDisconnect(int ref1, int ref2)
  128. {
  129. JackConnectionManager* manager = WriteNextStateStart();
  130. manager->DirectDisconnect(ref1, ref2);
  131. jack_log("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  132. WriteNextStateStop();
  133. }
  134. // Server
  135. bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
  136. {
  137. JackConnectionManager* manager = ReadCurrentState();
  138. return manager->IsDirectConnection(ref1, ref2);
  139. }
  140. // RT
  141. void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size)
  142. {
  143. AssertPort(port_index);
  144. AssertBufferSize(buffer_size);
  145. JackConnectionManager* manager = ReadCurrentState();
  146. JackPort* port = GetPort(port_index);
  147. // This happens when a port has just been unregistered and is still used by the RT code
  148. if (!port->IsUsed()) {
  149. jack_log("JackGraphManager::GetBuffer : port = %ld is released state", port_index);
  150. return GetBuffer(0); // port_index 0 is not used
  151. }
  152. // Output port
  153. if (port->fFlags & JackPortIsOutput) {
  154. return (port->fTied != NO_PORT) ? GetBuffer(port->fTied, buffer_size) : GetBuffer(port_index);
  155. }
  156. // Input port
  157. jack_int_t len = manager->Connections(port_index);
  158. // No connections : return a zero-filled buffer
  159. if (len == 0) {
  160. port->ClearBuffer(buffer_size);
  161. return port->GetBuffer();
  162. // One connection
  163. } else if (len == 1) {
  164. jack_port_id_t src_index = manager->GetPort(port_index, 0);
  165. // Ports in same client : copy the buffer
  166. if (GetPort(src_index)->GetRefNum() == port->GetRefNum()) {
  167. void* buffers[1];
  168. buffers[0] = GetBuffer(src_index, buffer_size);
  169. port->MixBuffers(buffers, 1, buffer_size);
  170. return port->GetBuffer();
  171. // Otherwise, use zero-copy mode, just pass the buffer of the connected (output) port.
  172. } else {
  173. return GetBuffer(src_index, buffer_size);
  174. }
  175. // Multiple connections : mix all buffers
  176. } else {
  177. const jack_int_t* connections = manager->GetConnections(port_index);
  178. void* buffers[CONNECTION_NUM_FOR_PORT];
  179. jack_port_id_t src_index;
  180. int i;
  181. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  182. AssertPort(src_index);
  183. buffers[i] = GetBuffer(src_index, buffer_size);
  184. }
  185. port->MixBuffers(buffers, i, buffer_size);
  186. return port->GetBuffer();
  187. }
  188. }
  189. // Server
  190. int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
  191. {
  192. AssertPort(port_index);
  193. JackPort* port = GetPort(port_index);
  194. /**
  195. jackd.h
  196. * If @ref JackPortCanMonitor is set for this @a port, turn input
  197. * monitoring on or off. Otherwise, do nothing.
  198. if (!(fFlags & JackPortCanMonitor))
  199. return -1;
  200. */
  201. port->RequestMonitor(onoff);
  202. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  203. if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags & JackPortIsInput) ?
  204. jack_port_id_t src_index;
  205. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  206. // XXX much worse things will happen if there is a feedback loop !!!
  207. RequestMonitor(src_index, onoff);
  208. }
  209. }
  210. return 0;
  211. }
  212. // Client
  213. jack_nframes_t JackGraphManager::ComputeTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
  214. {
  215. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  216. jack_nframes_t max_latency = 0;
  217. jack_port_id_t dst_index;
  218. if (hop_count > 8)
  219. return GetPort(port_index)->GetLatency();
  220. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  221. if (src_port_index != dst_index) {
  222. AssertPort(dst_index);
  223. JackPort* dst_port = GetPort(dst_index);
  224. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  225. ? dst_port->GetLatency()
  226. : ComputeTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  227. max_latency = ((max_latency > this_latency) ? max_latency : this_latency);
  228. }
  229. }
  230. return max_latency + GetPort(port_index)->GetLatency();
  231. }
  232. // Client
  233. int JackGraphManager::ComputeTotalLatency(jack_port_id_t port_index)
  234. {
  235. UInt16 cur_index;
  236. UInt16 next_index;
  237. JackPort* port = GetPort(port_index);
  238. AssertPort(port_index);
  239. do {
  240. cur_index = GetCurrentIndex();
  241. port->fTotalLatency = ComputeTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  242. next_index = GetCurrentIndex();
  243. } while (cur_index != next_index); // Until a coherent state has been read
  244. jack_log("JackGraphManager::GetTotalLatency port_index = %ld total latency = %ld", port_index, port->fTotalLatency);
  245. return 0;
  246. }
  247. // Client
  248. int JackGraphManager::ComputeTotalLatencies()
  249. {
  250. jack_port_id_t port_index;
  251. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  252. JackPort* port = GetPort(port_index);
  253. if (port->IsUsed())
  254. ComputeTotalLatency(port_index);
  255. }
  256. return 0;
  257. }
  258. void JackGraphManager::RecalculateLatencyAux(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  259. {
  260. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  261. JackPort* port = GetPort(port_index);
  262. jack_latency_range_t latency = { UINT32_MAX, 0 };
  263. jack_port_id_t dst_index;
  264. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  265. AssertPort(dst_index);
  266. JackPort* dst_port = GetPort(dst_index);
  267. jack_latency_range_t other_latency;
  268. dst_port->GetLatencyRange(mode, &other_latency);
  269. if (other_latency.max > latency.max)
  270. latency.max = other_latency.max;
  271. if (other_latency.min < latency.min)
  272. latency.min = other_latency.min;
  273. }
  274. if (latency.min == UINT32_MAX)
  275. latency.min = 0;
  276. port->SetLatencyRange(mode, &latency);
  277. }
  278. void JackGraphManager::RecalculateLatency(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  279. {
  280. UInt16 cur_index;
  281. UInt16 next_index;
  282. do {
  283. cur_index = GetCurrentIndex();
  284. RecalculateLatencyAux(port_index, mode);
  285. next_index = GetCurrentIndex();
  286. } while (cur_index != next_index); // Until a coherent state has been read
  287. jack_log("JackGraphManager::RecalculateLatency port_index = %ld", port_index);
  288. }
  289. // Server
  290. void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
  291. {
  292. jack_log("JackGraphManager::SetBufferSize size = %ld", buffer_size);
  293. jack_port_id_t port_index;
  294. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  295. JackPort* port = GetPort(port_index);
  296. if (port->IsUsed())
  297. port->ClearBuffer(buffer_size);
  298. }
  299. }
  300. // Server
  301. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  302. {
  303. jack_port_id_t port_index;
  304. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  305. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  306. JackPort* port = GetPort(port_index);
  307. if (!port->IsUsed()) {
  308. jack_log("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s", port_index, port_name, port_type);
  309. if (!port->Allocate(refnum, port_name, port_type, flags))
  310. return NO_PORT;
  311. break;
  312. }
  313. }
  314. return (port_index < fPortMax) ? port_index : NO_PORT;
  315. }
  316. // Server
  317. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags, jack_nframes_t buffer_size)
  318. {
  319. JackConnectionManager* manager = WriteNextStateStart();
  320. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);
  321. if (port_index != NO_PORT) {
  322. JackPort* port = GetPort(port_index);
  323. assert(port);
  324. port->ClearBuffer(buffer_size);
  325. int res;
  326. if (flags & JackPortIsOutput) {
  327. res = manager->AddOutputPort(refnum, port_index);
  328. } else {
  329. res = manager->AddInputPort(refnum, port_index);
  330. }
  331. // Insertion failure
  332. if (res < 0) {
  333. port->Release();
  334. port_index = NO_PORT;
  335. }
  336. }
  337. WriteNextStateStop();
  338. return port_index;
  339. }
  340. // Server
  341. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  342. {
  343. JackConnectionManager* manager = WriteNextStateStart();
  344. JackPort* port = GetPort(port_index);
  345. int res;
  346. if (port->fFlags & JackPortIsOutput) {
  347. DisconnectAllOutput(port_index);
  348. res = manager->RemoveOutputPort(refnum, port_index);
  349. } else {
  350. DisconnectAllInput(port_index);
  351. res = manager->RemoveInputPort(refnum, port_index);
  352. }
  353. port->Release();
  354. WriteNextStateStop();
  355. return res;
  356. }
  357. void JackGraphManager::GetInputPorts(int refnum, jack_int_t* res)
  358. {
  359. JackConnectionManager* manager = WriteNextStateStart();
  360. const jack_int_t* input = manager->GetInputPorts(refnum);
  361. memcpy(res, input, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  362. WriteNextStateStop();
  363. }
  364. void JackGraphManager::GetOutputPorts(int refnum, jack_int_t* res)
  365. {
  366. JackConnectionManager* manager = WriteNextStateStart();
  367. const jack_int_t* output = manager->GetOutputPorts(refnum);
  368. memcpy(res, output, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  369. WriteNextStateStop();
  370. }
  371. // Server
  372. void JackGraphManager::RemoveAllPorts(int refnum)
  373. {
  374. jack_log("JackGraphManager::RemoveAllPorts ref = %ld", refnum);
  375. JackConnectionManager* manager = WriteNextStateStart();
  376. jack_port_id_t port_index;
  377. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "input" table is empty
  378. const jack_int_t* input = manager->GetInputPorts(refnum);
  379. while ((port_index = input[0]) != EMPTY) {
  380. int res = ReleasePort(refnum, port_index);
  381. if (res < 0) {
  382. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  383. assert(true);
  384. break;
  385. }
  386. }
  387. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "output" table is empty
  388. const jack_int_t* output = manager->GetOutputPorts(refnum);
  389. while ((port_index = output[0]) != EMPTY) {
  390. int res = ReleasePort(refnum, port_index);
  391. if (res < 0) {
  392. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  393. assert(true);
  394. break;
  395. }
  396. }
  397. WriteNextStateStop();
  398. }
  399. // Server
  400. void JackGraphManager::DisconnectAllPorts(int refnum)
  401. {
  402. int i;
  403. jack_log("JackGraphManager::DisconnectAllPorts ref = %ld", refnum);
  404. JackConnectionManager* manager = WriteNextStateStart();
  405. const jack_int_t* input = manager->GetInputPorts(refnum);
  406. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  407. DisconnectAllInput(input[i]);
  408. }
  409. const jack_int_t* output = manager->GetOutputPorts(refnum);
  410. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  411. DisconnectAllOutput(output[i]);
  412. }
  413. WriteNextStateStop();
  414. }
  415. // Server
  416. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  417. {
  418. jack_log("JackGraphManager::DisconnectAllInput port_index = %ld", port_index);
  419. JackConnectionManager* manager = WriteNextStateStart();
  420. for (unsigned int i = 0; i < fPortMax; i++) {
  421. if (manager->IsConnected(i, port_index)) {
  422. jack_log("JackGraphManager::Disconnect i = %ld port_index = %ld", i, port_index);
  423. Disconnect(i, port_index);
  424. }
  425. }
  426. WriteNextStateStop();
  427. }
  428. // Server
  429. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  430. {
  431. jack_log("JackGraphManager::DisconnectAllOutput port_index = %ld ", port_index);
  432. JackConnectionManager* manager = WriteNextStateStart();
  433. const jack_int_t* connections = manager->GetConnections(port_index);
  434. while (connections[0] != EMPTY) {
  435. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  436. }
  437. WriteNextStateStop();
  438. }
  439. // Server
  440. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  441. {
  442. AssertPort(port_index);
  443. JackPort* port = GetPort(port_index);
  444. if (port->fFlags & JackPortIsOutput) {
  445. DisconnectAllOutput(port_index);
  446. } else {
  447. DisconnectAllInput(port_index);
  448. }
  449. return 0;
  450. }
  451. // Server
  452. void JackGraphManager::GetConnections(jack_port_id_t port_index, jack_int_t* res)
  453. {
  454. JackConnectionManager* manager = WriteNextStateStart();
  455. const jack_int_t* connections = manager->GetConnections(port_index);
  456. memcpy(res, connections, sizeof(jack_int_t) * CONNECTION_NUM_FOR_PORT);
  457. WriteNextStateStop();
  458. }
  459. // Server
  460. void JackGraphManager::Activate(int refnum)
  461. {
  462. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  463. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  464. }
  465. /*
  466. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  467. (thus unactivated) state may happen where the client is still checked for its end.
  468. */
  469. // Server
  470. void JackGraphManager::Deactivate(int refnum)
  471. {
  472. // Disconnect only when needed
  473. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  474. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  475. } else {
  476. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  477. }
  478. // Disconnect only when needed
  479. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  480. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  481. } else {
  482. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  483. }
  484. }
  485. // Server
  486. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  487. {
  488. AssertPort(port_index);
  489. JackConnectionManager* manager = WriteNextStateStart();
  490. int res = manager->GetInputRefNum(port_index);
  491. WriteNextStateStop();
  492. return res;
  493. }
  494. // Server
  495. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  496. {
  497. AssertPort(port_index);
  498. JackConnectionManager* manager = WriteNextStateStart();
  499. int res = manager->GetOutputRefNum(port_index);
  500. WriteNextStateStop();
  501. return res;
  502. }
  503. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  504. {
  505. JackConnectionManager* manager = WriteNextStateStart();
  506. jack_log("JackGraphManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
  507. JackPort* src = GetPort(port_src);
  508. JackPort* dst = GetPort(port_dst);
  509. int res = 0;
  510. if (!src->fInUse || !dst->fInUse) {
  511. if (!src->fInUse)
  512. jack_error("JackGraphManager::Connect port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  513. if (!dst->fInUse)
  514. jack_error("JackGraphManager::Connect port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  515. res = -1;
  516. goto end;
  517. }
  518. if (src->fTypeId != dst->fTypeId) {
  519. jack_error("JackGraphManager::Connect different port types port_src = %ld port_dst = %ld", port_src, port_dst);
  520. res = -1;
  521. goto end;
  522. }
  523. if (manager->IsConnected(port_src, port_dst)) {
  524. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  525. res = EEXIST;
  526. goto end;
  527. }
  528. res = manager->Connect(port_src, port_dst);
  529. if (res < 0) {
  530. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  531. goto end;
  532. }
  533. res = manager->Connect(port_dst, port_src);
  534. if (res < 0) {
  535. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  536. goto end;
  537. }
  538. if (manager->IsLoopPath(port_src, port_dst)) {
  539. jack_log("JackGraphManager::Connect: LOOP detected");
  540. manager->IncFeedbackConnection(port_src, port_dst);
  541. } else {
  542. manager->IncDirectConnection(port_src, port_dst);
  543. }
  544. end:
  545. WriteNextStateStop();
  546. return res;
  547. }
  548. // Server
  549. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  550. {
  551. JackConnectionManager* manager = WriteNextStateStart();
  552. jack_log("JackGraphManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
  553. bool in_use_src = GetPort(port_src)->fInUse;
  554. bool in_use_dst = GetPort(port_dst)->fInUse;
  555. int res = 0;
  556. if (!in_use_src || !in_use_dst) {
  557. if (!in_use_src)
  558. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  559. if (!in_use_dst)
  560. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  561. res = -1;
  562. goto end;
  563. }
  564. if (!manager->IsConnected(port_src, port_dst)) {
  565. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  566. res = -1;
  567. goto end;
  568. }
  569. res = manager->Disconnect(port_src, port_dst);
  570. if (res < 0) {
  571. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  572. goto end;
  573. }
  574. res = manager->Disconnect(port_dst, port_src);
  575. if (res < 0) {
  576. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  577. goto end;
  578. }
  579. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  580. jack_log("JackGraphManager::Disconnect: FEEDBACK removed");
  581. manager->DecFeedbackConnection(port_src, port_dst);
  582. } else {
  583. manager->DecDirectConnection(port_src, port_dst);
  584. }
  585. end:
  586. WriteNextStateStop();
  587. return res;
  588. }
  589. // Client
  590. int JackGraphManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst)
  591. {
  592. JackConnectionManager* manager = ReadCurrentState();
  593. return manager->IsConnected(port_src, port_dst);
  594. }
  595. // Server
  596. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  597. {
  598. JackPort* src = GetPort(port_src);
  599. JackPort* dst = GetPort(port_dst);
  600. if ((dst->fFlags & JackPortIsInput) == 0) {
  601. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  602. return -1;
  603. }
  604. if ((src->fFlags & JackPortIsOutput) == 0) {
  605. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  606. return -1;
  607. }
  608. return 0;
  609. }
  610. int JackGraphManager::GetTwoPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  611. {
  612. jack_log("JackGraphManager::CheckConnect src_name = %s dst_name = %s", src_name, dst_name);
  613. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  614. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  615. return -1;
  616. }
  617. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  618. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  619. return -1;
  620. }
  621. return 0;
  622. }
  623. // Client : port array
  624. jack_port_id_t JackGraphManager::GetPort(const char* name)
  625. {
  626. for (unsigned int i = 0; i < fPortMax; i++) {
  627. JackPort* port = GetPort(i);
  628. if (port->IsUsed() && port->NameEquals(name))
  629. return i;
  630. }
  631. return NO_PORT;
  632. }
  633. /*!
  634. \brief Get the connection port name array.
  635. */
  636. // Client
  637. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  638. {
  639. const jack_int_t* connections = manager->GetConnections(port_index);
  640. jack_int_t index;
  641. int i;
  642. // Cleanup connection array
  643. memset(res, 0, sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  644. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((index = connections[i]) != EMPTY); i++) {
  645. JackPort* port = GetPort(index);
  646. res[i] = port->fName;
  647. }
  648. res[i] = NULL;
  649. }
  650. /*
  651. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  652. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  653. read to loop forever.
  654. */
  655. // Client
  656. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  657. {
  658. const char** res = (const char**)malloc(sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  659. UInt16 cur_index, next_index;
  660. if (!res)
  661. return NULL;
  662. do {
  663. cur_index = GetCurrentIndex();
  664. GetConnectionsAux(ReadCurrentState(), res, port_index);
  665. next_index = GetCurrentIndex();
  666. } while (cur_index != next_index); // Until a coherent state has been read
  667. if (res[0]) { // at least one connection
  668. return res;
  669. } else { // empty array, should return NULL
  670. free(res);
  671. return NULL;
  672. }
  673. }
  674. // Client
  675. void JackGraphManager::GetPortsAux(const char** matching_ports, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  676. {
  677. int match_cnt = 0;
  678. regex_t port_regex, type_regex;
  679. if (port_name_pattern && port_name_pattern[0]) {
  680. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  681. }
  682. if (type_name_pattern && type_name_pattern[0]) {
  683. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  684. }
  685. // Cleanup port array
  686. memset(matching_ports, 0, sizeof(char*) * fPortMax);
  687. for (unsigned int i = 0; i < fPortMax; i++) {
  688. bool matching = true;
  689. JackPort* port = GetPort(i);
  690. if (port->IsUsed()) {
  691. if (flags) {
  692. if ((port->fFlags & flags) != flags) {
  693. matching = false;
  694. }
  695. }
  696. if (matching && port_name_pattern && port_name_pattern[0]) {
  697. if (regexec(&port_regex, port->GetName(), 0, NULL, 0)) {
  698. matching = false;
  699. }
  700. }
  701. if (matching && type_name_pattern && type_name_pattern[0]) {
  702. if (regexec(&type_regex, port->GetType(), 0, NULL, 0)) {
  703. matching = false;
  704. }
  705. }
  706. if (matching) {
  707. matching_ports[match_cnt++] = port->fName;
  708. }
  709. }
  710. }
  711. matching_ports[match_cnt] = 0;
  712. if (port_name_pattern && port_name_pattern[0]) {
  713. regfree(&port_regex);
  714. }
  715. if (type_name_pattern && type_name_pattern[0]) {
  716. regfree(&type_regex);
  717. }
  718. }
  719. // Client
  720. /*
  721. Check that the state was not changed during the read operation.
  722. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  723. read to loop forever.
  724. */
  725. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  726. {
  727. const char** res = (const char**)malloc(sizeof(char*) * fPortMax);
  728. UInt16 cur_index, next_index;
  729. if (!res)
  730. return NULL;
  731. do {
  732. cur_index = GetCurrentIndex();
  733. GetPortsAux(res, port_name_pattern, type_name_pattern, flags);
  734. next_index = GetCurrentIndex();
  735. } while (cur_index != next_index); // Until a coherent state has been read
  736. if (res[0]) { // at least one port
  737. return res;
  738. } else {
  739. free(res); // empty array, should return NULL
  740. return NULL;
  741. }
  742. }
  743. // Server
  744. void JackGraphManager::Save(JackConnectionManager* dst)
  745. {
  746. JackConnectionManager* manager = WriteNextStateStart();
  747. memcpy(dst, manager, sizeof(JackConnectionManager));
  748. WriteNextStateStop();
  749. }
  750. // Server
  751. void JackGraphManager::Restore(JackConnectionManager* src)
  752. {
  753. JackConnectionManager* manager = WriteNextStateStart();
  754. memcpy(manager, src, sizeof(JackConnectionManager));
  755. WriteNextStateStop();
  756. }
  757. } // end of namespace