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.

909 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. jack_default_audio_sample_t* 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. jack_int_t len = manager->Connections(port_index);
  153. // Output port
  154. if (port->fFlags & JackPortIsOutput) {
  155. return (port->fTied != NO_PORT) ? GetBuffer(port->fTied, buffer_size) : GetBuffer(port_index);
  156. }
  157. // No connections : return a zero-filled buffer
  158. if (len == 0) {
  159. port->ClearBuffer(buffer_size);
  160. return port->GetBuffer();
  161. // One connection
  162. } else if (len == 1) {
  163. jack_port_id_t src_index = manager->GetPort(port_index, 0);
  164. // Ports in same client : copy the buffer
  165. if (GetPort(src_index)->GetRefNum() == port->GetRefNum()) {
  166. void* buffers[1];
  167. buffers[0] = GetBuffer(src_index, buffer_size);
  168. port->MixBuffers(buffers, 1, buffer_size);
  169. return port->GetBuffer();
  170. // Otherwise, use zero-copy mode, just pass the buffer of the connected (output) port.
  171. } else {
  172. return GetBuffer(src_index, buffer_size);
  173. }
  174. // Multiple connections : mix all buffers
  175. } else {
  176. const jack_int_t* connections = manager->GetConnections(port_index);
  177. void* buffers[CONNECTION_NUM_FOR_PORT];
  178. jack_port_id_t src_index;
  179. int i;
  180. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  181. AssertPort(src_index);
  182. buffers[i] = GetBuffer(src_index, buffer_size);
  183. }
  184. port->MixBuffers(buffers, i, buffer_size);
  185. return port->GetBuffer();
  186. }
  187. }
  188. // Server
  189. int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
  190. {
  191. AssertPort(port_index);
  192. JackPort* port = GetPort(port_index);
  193. /**
  194. jackd.h
  195. * If @ref JackPortCanMonitor is set for this @a port, turn input
  196. * monitoring on or off. Otherwise, do nothing.
  197. if (!(fFlags & JackPortCanMonitor))
  198. return -1;
  199. */
  200. port->RequestMonitor(onoff);
  201. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  202. if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags & JackPortIsInput) ?
  203. jack_port_id_t src_index;
  204. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  205. // XXX much worse things will happen if there is a feedback loop !!!
  206. RequestMonitor(src_index, onoff);
  207. }
  208. }
  209. return 0;
  210. }
  211. // Client
  212. jack_nframes_t JackGraphManager::ComputeTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
  213. {
  214. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  215. jack_nframes_t max_latency = 0;
  216. jack_port_id_t dst_index;
  217. if (hop_count > 8)
  218. return GetPort(port_index)->GetLatency();
  219. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  220. if (src_port_index != dst_index) {
  221. AssertPort(dst_index);
  222. JackPort* dst_port = GetPort(dst_index);
  223. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  224. ? dst_port->GetLatency()
  225. : ComputeTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  226. max_latency = ((max_latency > this_latency) ? max_latency : this_latency);
  227. }
  228. }
  229. return max_latency + GetPort(port_index)->GetLatency();
  230. }
  231. // Client
  232. int JackGraphManager::ComputeTotalLatency(jack_port_id_t port_index)
  233. {
  234. UInt16 cur_index;
  235. UInt16 next_index;
  236. JackPort* port = GetPort(port_index);
  237. AssertPort(port_index);
  238. do {
  239. cur_index = GetCurrentIndex();
  240. port->fTotalLatency = ComputeTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  241. next_index = GetCurrentIndex();
  242. } while (cur_index != next_index); // Until a coherent state has been read
  243. jack_log("JackGraphManager::GetTotalLatency port_index = %ld total latency = %ld", port_index, port->fTotalLatency);
  244. return 0;
  245. }
  246. // Client
  247. int JackGraphManager::ComputeTotalLatencies()
  248. {
  249. jack_port_id_t port_index;
  250. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  251. JackPort* port = GetPort(port_index);
  252. if (port->IsUsed()) {
  253. ComputeTotalLatency(port_index);
  254. }
  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. }
  272. if (other_latency.min < latency.min) {
  273. latency.min = other_latency.min;
  274. }
  275. }
  276. if (latency.min == UINT32_MAX) {
  277. latency.min = 0;
  278. }
  279. port->SetLatencyRange(mode, &latency);
  280. }
  281. void JackGraphManager::RecalculateLatency(jack_port_id_t port_index, jack_latency_callback_mode_t mode)
  282. {
  283. UInt16 cur_index;
  284. UInt16 next_index;
  285. do {
  286. cur_index = GetCurrentIndex();
  287. RecalculateLatencyAux(port_index, mode);
  288. next_index = GetCurrentIndex();
  289. } while (cur_index != next_index); // Until a coherent state has been read
  290. //jack_log("JackGraphManager::RecalculateLatency port_index = %ld", port_index);
  291. }
  292. // Server
  293. void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
  294. {
  295. jack_log("JackGraphManager::SetBufferSize size = %ld", buffer_size);
  296. jack_port_id_t port_index;
  297. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  298. JackPort* port = GetPort(port_index);
  299. if (port->IsUsed()) {
  300. port->ClearBuffer(buffer_size);
  301. }
  302. }
  303. }
  304. // Server
  305. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  306. {
  307. jack_port_id_t port_index;
  308. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  309. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  310. JackPort* port = GetPort(port_index);
  311. if (!port->IsUsed()) {
  312. jack_log("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s", port_index, port_name, port_type);
  313. if (!port->Allocate(refnum, port_name, port_type, flags)) {
  314. return NO_PORT;
  315. }
  316. break;
  317. }
  318. }
  319. return (port_index < fPortMax) ? port_index : NO_PORT;
  320. }
  321. // Server
  322. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags, jack_nframes_t buffer_size)
  323. {
  324. JackConnectionManager* manager = WriteNextStateStart();
  325. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);
  326. if (port_index != NO_PORT) {
  327. JackPort* port = GetPort(port_index);
  328. assert(port);
  329. port->ClearBuffer(buffer_size);
  330. int res;
  331. if (flags & JackPortIsOutput) {
  332. res = manager->AddOutputPort(refnum, port_index);
  333. } else {
  334. res = manager->AddInputPort(refnum, port_index);
  335. }
  336. // Insertion failure
  337. if (res < 0) {
  338. port->Release();
  339. port_index = NO_PORT;
  340. }
  341. }
  342. WriteNextStateStop();
  343. return port_index;
  344. }
  345. // Server
  346. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  347. {
  348. JackConnectionManager* manager = WriteNextStateStart();
  349. JackPort* port = GetPort(port_index);
  350. int res;
  351. if (port->fFlags & JackPortIsOutput) {
  352. DisconnectAllOutput(port_index);
  353. res = manager->RemoveOutputPort(refnum, port_index);
  354. } else {
  355. DisconnectAllInput(port_index);
  356. res = manager->RemoveInputPort(refnum, port_index);
  357. }
  358. port->Release();
  359. WriteNextStateStop();
  360. return res;
  361. }
  362. void JackGraphManager::GetInputPorts(int refnum, jack_int_t* res)
  363. {
  364. JackConnectionManager* manager = WriteNextStateStart();
  365. const jack_int_t* input = manager->GetInputPorts(refnum);
  366. memcpy(res, input, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  367. WriteNextStateStop();
  368. }
  369. void JackGraphManager::GetOutputPorts(int refnum, jack_int_t* res)
  370. {
  371. JackConnectionManager* manager = WriteNextStateStart();
  372. const jack_int_t* output = manager->GetOutputPorts(refnum);
  373. memcpy(res, output, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  374. WriteNextStateStop();
  375. }
  376. // Server
  377. void JackGraphManager::RemoveAllPorts(int refnum)
  378. {
  379. jack_log("JackGraphManager::RemoveAllPorts ref = %ld", refnum);
  380. JackConnectionManager* manager = WriteNextStateStart();
  381. jack_port_id_t port_index;
  382. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "input" table is empty
  383. const jack_int_t* input = manager->GetInputPorts(refnum);
  384. while ((port_index = input[0]) != EMPTY) {
  385. int res = ReleasePort(refnum, port_index);
  386. if (res < 0) {
  387. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  388. assert(true);
  389. break;
  390. }
  391. }
  392. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "output" table is empty
  393. const jack_int_t* output = manager->GetOutputPorts(refnum);
  394. while ((port_index = output[0]) != EMPTY) {
  395. int res = ReleasePort(refnum, port_index);
  396. if (res < 0) {
  397. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  398. assert(true);
  399. break;
  400. }
  401. }
  402. WriteNextStateStop();
  403. }
  404. // Server
  405. void JackGraphManager::DisconnectAllPorts(int refnum)
  406. {
  407. int i;
  408. jack_log("JackGraphManager::DisconnectAllPorts ref = %ld", refnum);
  409. JackConnectionManager* manager = WriteNextStateStart();
  410. const jack_int_t* input = manager->GetInputPorts(refnum);
  411. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  412. DisconnectAllInput(input[i]);
  413. }
  414. const jack_int_t* output = manager->GetOutputPorts(refnum);
  415. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  416. DisconnectAllOutput(output[i]);
  417. }
  418. WriteNextStateStop();
  419. }
  420. // Server
  421. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  422. {
  423. jack_log("JackGraphManager::DisconnectAllInput port_index = %ld", port_index);
  424. JackConnectionManager* manager = WriteNextStateStart();
  425. for (unsigned int i = 0; i < fPortMax; i++) {
  426. if (manager->IsConnected(i, port_index)) {
  427. jack_log("JackGraphManager::Disconnect i = %ld port_index = %ld", i, port_index);
  428. Disconnect(i, port_index);
  429. }
  430. }
  431. WriteNextStateStop();
  432. }
  433. // Server
  434. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  435. {
  436. jack_log("JackGraphManager::DisconnectAllOutput port_index = %ld ", port_index);
  437. JackConnectionManager* manager = WriteNextStateStart();
  438. const jack_int_t* connections = manager->GetConnections(port_index);
  439. while (connections[0] != EMPTY) {
  440. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  441. }
  442. WriteNextStateStop();
  443. }
  444. // Server
  445. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  446. {
  447. AssertPort(port_index);
  448. JackPort* port = GetPort(port_index);
  449. if (port->fFlags & JackPortIsOutput) {
  450. DisconnectAllOutput(port_index);
  451. } else {
  452. DisconnectAllInput(port_index);
  453. }
  454. return 0;
  455. }
  456. // Server
  457. void JackGraphManager::GetConnections(jack_port_id_t port_index, jack_int_t* res)
  458. {
  459. JackConnectionManager* manager = WriteNextStateStart();
  460. const jack_int_t* connections = manager->GetConnections(port_index);
  461. memcpy(res, connections, sizeof(jack_int_t) * CONNECTION_NUM_FOR_PORT);
  462. WriteNextStateStop();
  463. }
  464. // Server
  465. void JackGraphManager::Activate(int refnum)
  466. {
  467. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  468. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  469. }
  470. /*
  471. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  472. (thus unactivated) state may happen where the client is still checked for its end.
  473. */
  474. // Server
  475. void JackGraphManager::Deactivate(int refnum)
  476. {
  477. // Disconnect only when needed
  478. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  479. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  480. } else {
  481. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  482. }
  483. // Disconnect only when needed
  484. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  485. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  486. } else {
  487. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  488. }
  489. }
  490. // Server
  491. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  492. {
  493. AssertPort(port_index);
  494. JackConnectionManager* manager = WriteNextStateStart();
  495. int res = manager->GetInputRefNum(port_index);
  496. WriteNextStateStop();
  497. return res;
  498. }
  499. // Server
  500. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  501. {
  502. AssertPort(port_index);
  503. JackConnectionManager* manager = WriteNextStateStart();
  504. int res = manager->GetOutputRefNum(port_index);
  505. WriteNextStateStop();
  506. return res;
  507. }
  508. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  509. {
  510. JackConnectionManager* manager = WriteNextStateStart();
  511. jack_log("JackGraphManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
  512. JackPort* src = GetPort(port_src);
  513. JackPort* dst = GetPort(port_dst);
  514. int res = 0;
  515. if (!src->fInUse || !dst->fInUse) {
  516. if (!src->fInUse)
  517. jack_error("JackGraphManager::Connect port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  518. if (!dst->fInUse)
  519. jack_error("JackGraphManager::Connect port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  520. res = -1;
  521. goto end;
  522. }
  523. if (src->fTypeId != dst->fTypeId) {
  524. jack_error("JackGraphManager::Connect different port types port_src = %ld port_dst = %ld", port_src, port_dst);
  525. res = -1;
  526. goto end;
  527. }
  528. if (manager->IsConnected(port_src, port_dst)) {
  529. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  530. res = EEXIST;
  531. goto end;
  532. }
  533. res = manager->Connect(port_src, port_dst);
  534. if (res < 0) {
  535. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  536. goto end;
  537. }
  538. res = manager->Connect(port_dst, port_src);
  539. if (res < 0) {
  540. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  541. goto end;
  542. }
  543. if (manager->IsLoopPath(port_src, port_dst)) {
  544. jack_log("JackGraphManager::Connect: LOOP detected");
  545. manager->IncFeedbackConnection(port_src, port_dst);
  546. } else {
  547. manager->IncDirectConnection(port_src, port_dst);
  548. }
  549. end:
  550. WriteNextStateStop();
  551. return res;
  552. }
  553. // Server
  554. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  555. {
  556. JackConnectionManager* manager = WriteNextStateStart();
  557. jack_log("JackGraphManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
  558. bool in_use_src = GetPort(port_src)->fInUse;
  559. bool in_use_dst = GetPort(port_dst)->fInUse;
  560. int res = 0;
  561. if (!in_use_src || !in_use_dst) {
  562. if (!in_use_src)
  563. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  564. if (!in_use_dst)
  565. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  566. res = -1;
  567. goto end;
  568. }
  569. if (!manager->IsConnected(port_src, port_dst)) {
  570. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  571. res = -1;
  572. goto end;
  573. }
  574. res = manager->Disconnect(port_src, port_dst);
  575. if (res < 0) {
  576. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  577. goto end;
  578. }
  579. res = manager->Disconnect(port_dst, port_src);
  580. if (res < 0) {
  581. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  582. goto end;
  583. }
  584. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  585. jack_log("JackGraphManager::Disconnect: FEEDBACK removed");
  586. manager->DecFeedbackConnection(port_src, port_dst);
  587. } else {
  588. manager->DecDirectConnection(port_src, port_dst);
  589. }
  590. end:
  591. WriteNextStateStop();
  592. return res;
  593. }
  594. // Client
  595. int JackGraphManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst)
  596. {
  597. JackConnectionManager* manager = ReadCurrentState();
  598. return manager->IsConnected(port_src, port_dst);
  599. }
  600. // Server
  601. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  602. {
  603. JackPort* src = GetPort(port_src);
  604. JackPort* dst = GetPort(port_dst);
  605. if ((dst->fFlags & JackPortIsInput) == 0) {
  606. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  607. return -1;
  608. }
  609. if ((src->fFlags & JackPortIsOutput) == 0) {
  610. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  611. return -1;
  612. }
  613. return 0;
  614. }
  615. int JackGraphManager::GetTwoPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  616. {
  617. jack_log("JackGraphManager::CheckConnect src_name = %s dst_name = %s", src_name, dst_name);
  618. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  619. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  620. return -1;
  621. }
  622. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  623. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  624. return -1;
  625. }
  626. return 0;
  627. }
  628. // Client : port array
  629. jack_port_id_t JackGraphManager::GetPort(const char* name)
  630. {
  631. for (unsigned int i = 0; i < fPortMax; i++) {
  632. JackPort* port = GetPort(i);
  633. if (port->IsUsed() && port->NameEquals(name)) {
  634. return i;
  635. }
  636. }
  637. return NO_PORT;
  638. }
  639. /*!
  640. \brief Get the connection port name array.
  641. */
  642. // Client
  643. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  644. {
  645. const jack_int_t* connections = manager->GetConnections(port_index);
  646. jack_int_t index;
  647. int i;
  648. // Cleanup connection array
  649. memset(res, 0, sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  650. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((index = connections[i]) != EMPTY); i++) {
  651. JackPort* port = GetPort(index);
  652. res[i] = port->fName;
  653. }
  654. res[i] = NULL;
  655. }
  656. /*
  657. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  658. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  659. read to loop forever.
  660. */
  661. // Client
  662. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  663. {
  664. const char** res = (const char**)malloc(sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  665. UInt16 cur_index, next_index;
  666. if (!res)
  667. return NULL;
  668. do {
  669. cur_index = GetCurrentIndex();
  670. GetConnectionsAux(ReadCurrentState(), res, port_index);
  671. next_index = GetCurrentIndex();
  672. } while (cur_index != next_index); // Until a coherent state has been read
  673. if (res[0]) { // At least one connection
  674. return res;
  675. } else { // Empty array, should return NULL
  676. free(res);
  677. return NULL;
  678. }
  679. }
  680. // Client
  681. void JackGraphManager::GetPortsAux(const char** matching_ports, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  682. {
  683. int match_cnt = 0;
  684. regex_t port_regex, type_regex;
  685. if (port_name_pattern && port_name_pattern[0]) {
  686. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  687. }
  688. if (type_name_pattern && type_name_pattern[0]) {
  689. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  690. }
  691. // Cleanup port array
  692. memset(matching_ports, 0, sizeof(char*) * fPortMax);
  693. for (unsigned int i = 0; i < fPortMax; i++) {
  694. bool matching = true;
  695. JackPort* port = GetPort(i);
  696. if (port->IsUsed()) {
  697. if (flags) {
  698. if ((port->fFlags & flags) != flags) {
  699. matching = false;
  700. }
  701. }
  702. if (matching && port_name_pattern && port_name_pattern[0]) {
  703. if (regexec(&port_regex, port->GetName(), 0, NULL, 0)) {
  704. matching = false;
  705. }
  706. }
  707. if (matching && type_name_pattern && type_name_pattern[0]) {
  708. if (regexec(&type_regex, port->GetType(), 0, NULL, 0)) {
  709. matching = false;
  710. }
  711. }
  712. if (matching) {
  713. matching_ports[match_cnt++] = port->fName;
  714. }
  715. }
  716. }
  717. matching_ports[match_cnt] = 0;
  718. if (port_name_pattern && port_name_pattern[0]) {
  719. regfree(&port_regex);
  720. }
  721. if (type_name_pattern && type_name_pattern[0]) {
  722. regfree(&type_regex);
  723. }
  724. }
  725. // Client
  726. /*
  727. Check that the state was not changed during the read operation.
  728. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  729. read to loop forever.
  730. */
  731. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  732. {
  733. const char** res = (const char**)malloc(sizeof(char*) * fPortMax);
  734. UInt16 cur_index, next_index;
  735. if (!res)
  736. return NULL;
  737. do {
  738. cur_index = GetCurrentIndex();
  739. GetPortsAux(res, port_name_pattern, type_name_pattern, flags);
  740. next_index = GetCurrentIndex();
  741. } while (cur_index != next_index); // Until a coherent state has been read
  742. if (res[0]) { // At least one port
  743. return res;
  744. } else {
  745. free(res); // Empty array, should return NULL
  746. return NULL;
  747. }
  748. }
  749. // Server
  750. void JackGraphManager::Save(JackConnectionManager* dst)
  751. {
  752. JackConnectionManager* manager = WriteNextStateStart();
  753. memcpy(dst, manager, sizeof(JackConnectionManager));
  754. WriteNextStateStop();
  755. }
  756. // Server
  757. void JackGraphManager::Restore(JackConnectionManager* src)
  758. {
  759. JackConnectionManager* manager = WriteNextStateStart();
  760. memcpy(manager, src, sizeof(JackConnectionManager));
  761. WriteNextStateStop();
  762. }
  763. } // end of namespace