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.

771 lines
24KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 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.
  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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackGraphManager.h"
  17. #include "JackConstants.h"
  18. #include <assert.h>
  19. #include <stdlib.h>
  20. #include <algorithm>
  21. #include <regex.h>
  22. namespace Jack
  23. {
  24. static inline jack_nframes_t MAX(jack_nframes_t a, jack_nframes_t b)
  25. {
  26. return (a < b) ? b : a;
  27. }
  28. static void AssertPort(jack_port_id_t port_index)
  29. {
  30. if (port_index >= PORT_NUM) {
  31. JackLog("JackGraphManager::AssertPort port_index = %ld\n", port_index);
  32. assert(port_index < PORT_NUM);
  33. }
  34. }
  35. static void AssertBufferSize(jack_nframes_t buffer_size)
  36. {
  37. if (buffer_size > BUFFER_SIZE_MAX) {
  38. JackLog("JackGraphManager::AssertBufferSize frames = %ld\n", buffer_size);
  39. assert(buffer_size <= BUFFER_SIZE_MAX);
  40. }
  41. }
  42. JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
  43. {
  44. AssertPort(port_index);
  45. return &fPortArray[port_index];
  46. }
  47. float* JackGraphManager::GetBuffer(jack_port_id_t port_index)
  48. {
  49. return fPortArray[port_index].GetBuffer();
  50. }
  51. // RT, client
  52. int JackGraphManager::GetConnectionsNum(jack_port_id_t port_index)
  53. {
  54. JackConnectionManager* manager = ReadCurrentState();
  55. return manager->Connections(port_index);
  56. }
  57. // Server
  58. void JackGraphManager::InitRefNum(int refnum)
  59. {
  60. JackConnectionManager* manager = WriteNextStateStart();
  61. manager->InitRefNum(refnum);
  62. WriteNextStateStop();
  63. }
  64. // RT
  65. void JackGraphManager::RunCurrentGraph()
  66. {
  67. JackConnectionManager* manager = ReadCurrentState();
  68. manager->ResetGraph(fClientTiming);
  69. }
  70. // RT
  71. bool JackGraphManager::RunNextGraph()
  72. {
  73. bool res;
  74. JackConnectionManager* manager = TrySwitchState(&res);
  75. manager->ResetGraph(fClientTiming);
  76. return res;
  77. }
  78. // RT
  79. bool JackGraphManager::IsFinishedGraph()
  80. {
  81. JackConnectionManager* manager = ReadCurrentState();
  82. return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
  83. }
  84. // RT
  85. int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro** table)
  86. {
  87. JackConnectionManager* manager = ReadCurrentState();
  88. return manager->ResumeRefNum(control, table, fClientTiming);
  89. }
  90. // RT
  91. int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro** table, long usec)
  92. {
  93. JackConnectionManager* manager = ReadCurrentState();
  94. return manager->SuspendRefNum(control, table, fClientTiming, usec);
  95. }
  96. JackClientTiming* JackGraphManager::GetClientTiming(int ref)
  97. {
  98. return &fClientTiming[ref];
  99. }
  100. // Server
  101. void JackGraphManager::DirectConnect(int ref1, int ref2)
  102. {
  103. JackConnectionManager* manager = WriteNextStateStart();
  104. manager->DirectConnect(ref1, ref2);
  105. JackLog("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld\n", CurIndex(fCounter), ref1, ref2);
  106. WriteNextStateStop();
  107. }
  108. // Server
  109. void JackGraphManager::DirectDisconnect(int ref1, int ref2)
  110. {
  111. JackConnectionManager* manager = WriteNextStateStart();
  112. manager->DirectDisconnect(ref1, ref2);
  113. JackLog("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld\n", CurIndex(fCounter), ref1, ref2);
  114. WriteNextStateStop();
  115. }
  116. // Server
  117. bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
  118. {
  119. JackConnectionManager* manager = ReadCurrentState();
  120. return manager->IsDirectConnection(ref1, ref2);
  121. }
  122. // RT
  123. void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size)
  124. {
  125. AssertPort(port_index);
  126. AssertBufferSize(buffer_size);
  127. JackConnectionManager* manager = ReadCurrentState();
  128. JackPort* port = GetPort(port_index);
  129. if (!port->IsUsed()) {
  130. // This happens when a port has just been unregistered and is still used by the RT code.
  131. JackLog("JackGraphManager::GetBuffer : port = %ld is released state\n", port_index);
  132. return GetBuffer(0); // port_index 0 is not used
  133. }
  134. // Output port
  135. if (port->fFlags & JackPortIsOutput) {
  136. return (port->fTied != NO_PORT) ? GetBuffer(port->fTied, buffer_size) : GetBuffer(port_index);
  137. }
  138. // Input port
  139. jack_int_t len = manager->Connections(port_index);
  140. if (len == 0) { // No connections: return a zero-filled buffer
  141. float* buffer = GetBuffer(port_index);
  142. memset(buffer, 0, buffer_size * sizeof(float)); // Clear buffer
  143. return buffer;
  144. } else if (len == 1) { // One connection: use zero-copy mode - just pass the buffer of the connected (output) port.
  145. assert(manager->GetPort(port_index, 0) != port_index); // Check recursion
  146. return GetBuffer(manager->GetPort(port_index, 0), buffer_size);
  147. } else { // Multiple connections
  148. const jack_int_t* connections = manager->GetConnections(port_index);
  149. float* mixbuffer = GetBuffer(port_index);
  150. jack_port_id_t src_index;
  151. float* buffer;
  152. // Copy first buffer
  153. src_index = connections[0];
  154. AssertPort(src_index);
  155. buffer = (float*)GetBuffer(src_index, buffer_size);
  156. memcpy(mixbuffer, buffer, buffer_size * sizeof(float));
  157. // Mix remaining buffers
  158. for (int i = 1; (i < CONNECTION_NUM) && ((src_index = connections[i]) != EMPTY); i++) {
  159. AssertPort(src_index);
  160. buffer = (float*)GetBuffer(src_index, buffer_size);
  161. JackPort::MixBuffer(mixbuffer, buffer, buffer_size);
  162. }
  163. return mixbuffer;
  164. }
  165. }
  166. int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
  167. {
  168. AssertPort(port_index);
  169. JackPort* port = GetPort(port_index);
  170. /**
  171. jackd.h
  172. * If @ref JackPortCanMonitor is set for this @a port, turn input
  173. * monitoring on or off. Otherwise, do nothing.
  174. if (!(fFlags & JackPortCanMonitor))
  175. return -1;
  176. */
  177. port->RequestMonitor(onoff);
  178. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  179. if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags & JackPortIsInput) ?
  180. jack_port_id_t src_index;
  181. for (int i = 0; (i < CONNECTION_NUM) && ((src_index = connections[i]) != EMPTY); i++) {
  182. // XXX much worse things will happen if there is a feedback loop !!!
  183. RequestMonitor(src_index, onoff);
  184. }
  185. }
  186. return 0;
  187. }
  188. jack_nframes_t JackGraphManager::GetTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
  189. {
  190. const jack_int_t* connections = manager->GetConnections(port_index);
  191. jack_nframes_t latency = GetPort(port_index)->GetLatency();
  192. jack_nframes_t max_latency = 0;
  193. jack_port_id_t dst_index;
  194. if (hop_count > 8)
  195. return latency;
  196. for (int i = 0; (i < CONNECTION_NUM) && ((dst_index = connections[i]) != EMPTY); i++) {
  197. if (src_port_index != dst_index) {
  198. AssertPort(dst_index);
  199. JackPort* dst_port = GetPort(dst_index);
  200. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  201. ? dst_port->GetLatency()
  202. : GetTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  203. max_latency = MAX(max_latency, this_latency);
  204. }
  205. }
  206. return max_latency + latency;
  207. }
  208. jack_nframes_t JackGraphManager::GetTotalLatency(jack_port_id_t port_index)
  209. {
  210. UInt16 cur_index;
  211. UInt16 next_index;
  212. jack_nframes_t total_latency;
  213. AssertPort(port_index);
  214. JackLog("JackGraphManager::GetTotalLatency port_index = %ld\n", port_index);
  215. do {
  216. cur_index = GetCurrentIndex();
  217. total_latency = GetTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  218. next_index = GetCurrentIndex();
  219. } while (cur_index != next_index); // Until a coherent state has been read
  220. return total_latency;
  221. }
  222. // Server
  223. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, JackPortFlags flags)
  224. {
  225. jack_port_id_t port_index;
  226. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  227. for (port_index = FIRST_AVAILABLE_PORT; port_index < PORT_NUM; port_index++) {
  228. JackPort* port = GetPort(port_index);
  229. if (!port->IsUsed()) {
  230. JackLog("JackGraphManager::AllocatePortAux port_index = %ld name = %s\n", port_index, port_name);
  231. port->Allocate(refnum, port_name, flags);
  232. break;
  233. }
  234. }
  235. return (port_index < PORT_NUM) ? port_index : NO_PORT;
  236. }
  237. // Server
  238. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, JackPortFlags flags)
  239. {
  240. JackConnectionManager* manager = WriteNextStateStart();
  241. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, flags);
  242. if (port_index != NO_PORT) {
  243. int res;
  244. if (flags & JackPortIsOutput) {
  245. res = manager->AddOutputPort(refnum, port_index);
  246. } else {
  247. res = manager->AddInputPort(refnum, port_index);
  248. }
  249. if (res < 0) {
  250. JackPort* port = GetPort(port_index);
  251. assert(port);
  252. port->Release();
  253. port_index = NO_PORT;
  254. }
  255. }
  256. WriteNextStateStop();
  257. return port_index;
  258. }
  259. // Server
  260. void JackGraphManager::ReleasePort(jack_port_id_t port_index)
  261. {
  262. JackPort* port = GetPort(port_index);
  263. port->Release();
  264. }
  265. // Server
  266. int JackGraphManager::RemovePort(int refnum, jack_port_id_t port_index)
  267. {
  268. JackConnectionManager* manager = WriteNextStateStart();
  269. JackPort* port = GetPort(port_index);
  270. int res;
  271. if (port->fFlags & JackPortIsOutput) {
  272. DisconnectAllOutput(port_index);
  273. res = manager->RemoveOutputPort(refnum, port_index);
  274. } else {
  275. DisconnectAllInput(port_index);
  276. res = manager->RemoveInputPort(refnum, port_index);
  277. }
  278. WriteNextStateStop();
  279. return res;
  280. }
  281. // Server
  282. void JackGraphManager::RemoveAllPorts(int refnum)
  283. {
  284. JackLog("JackGraphManager::RemoveAllPorts ref = %ld\n", refnum);
  285. JackConnectionManager* manager = WriteNextStateStart();
  286. jack_port_id_t port_index;
  287. // Warning : RemovePort shift port to left, thus we always remove the first port until the "input" table is empty
  288. const jack_int_t* input = manager->GetInputPorts(refnum);
  289. while ((port_index = input[0]) != EMPTY) {
  290. RemovePort(refnum, port_index);
  291. ReleasePort(port_index);
  292. }
  293. // Warning : RemovePort shift port to left, thus we always remove the first port until the "output" table is empty
  294. const jack_int_t* output = manager->GetOutputPorts(refnum);
  295. while ((port_index = output[0]) != EMPTY) {
  296. RemovePort(refnum, port_index);
  297. ReleasePort(port_index);
  298. }
  299. WriteNextStateStop();
  300. }
  301. // Server
  302. void JackGraphManager::DisconnectAllPorts(int refnum)
  303. {
  304. int i;
  305. JackLog("JackGraphManager::DisconnectAllPorts ref = %ld\n", refnum);
  306. JackConnectionManager* manager = WriteNextStateStart();
  307. const jack_int_t* input = manager->GetInputPorts(refnum);
  308. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  309. DisconnectAllInput(input[i]);
  310. }
  311. const jack_int_t* output = manager->GetOutputPorts(refnum);
  312. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  313. DisconnectAllOutput(output[i]);
  314. }
  315. WriteNextStateStop();
  316. }
  317. // Server
  318. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  319. {
  320. JackLog("JackGraphManager::DisconnectAllInput port_index = %ld \n", port_index);
  321. JackConnectionManager* manager = WriteNextStateStart();
  322. for (int i = 0; i < PORT_NUM; i++) {
  323. if (manager->IsConnected(i, port_index)) {
  324. JackLog("JackGraphManager::Disconnect i = %ld port_index = %ld\n", i, port_index);
  325. Disconnect(i, port_index);
  326. }
  327. }
  328. WriteNextStateStop();
  329. }
  330. // Server
  331. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  332. {
  333. JackLog("JackGraphManager::DisconnectAllOutput port_index = %ld \n", port_index);
  334. JackConnectionManager* manager = WriteNextStateStart();
  335. const jack_int_t* connections = manager->GetConnections(port_index);
  336. while (connections[0] != EMPTY) {
  337. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  338. }
  339. WriteNextStateStop();
  340. }
  341. // Server
  342. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  343. {
  344. AssertPort(port_index);
  345. JackPort* port = GetPort(port_index);
  346. if (port->fFlags & JackPortIsOutput) {
  347. DisconnectAllOutput(port_index);
  348. } else {
  349. DisconnectAllInput(port_index);
  350. }
  351. return 0;
  352. }
  353. // Server
  354. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  355. {
  356. AssertPort(port_index);
  357. JackConnectionManager* manager = WriteNextStateStart();
  358. int res = manager->GetInputRefNum(port_index);
  359. WriteNextStateStop();
  360. return res;
  361. }
  362. // Server
  363. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  364. {
  365. AssertPort(port_index);
  366. JackConnectionManager* manager = WriteNextStateStart();
  367. int res = manager->GetOutputRefNum(port_index);
  368. WriteNextStateStop();
  369. return res;
  370. }
  371. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  372. {
  373. JackConnectionManager* manager = WriteNextStateStart();
  374. JackLog("JackGraphManager::Connect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  375. bool in_use_src = GetPort(port_src)->fInUse;
  376. bool in_use_dst = GetPort(port_src)->fInUse;
  377. int res = 0;
  378. if (!in_use_src || !in_use_dst) {
  379. if (!in_use_src)
  380. jack_error("JackGraphManager::Connect: port_src not %ld used name = %s", port_src, GetPort(port_src)->fName);
  381. if (!in_use_dst)
  382. jack_error("JackGraphManager::Connect: port_dst not %ld used name = %s", port_dst, GetPort(port_dst)->fName);
  383. res = -1;
  384. goto end;
  385. }
  386. if (manager->IsConnected(port_src, port_dst)) {
  387. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  388. res = EEXIST;
  389. goto end;
  390. }
  391. res = manager->Connect(port_src, port_dst);
  392. if (res < 0) {
  393. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  394. goto end;
  395. }
  396. manager->Connect(port_dst, port_src);
  397. if (res < 0) {
  398. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_dst, port_src);
  399. goto end;
  400. }
  401. if (manager->IsLoopPath(port_src, port_dst)) {
  402. JackLog("JackGraphManager::Connect: LOOP detected\n");
  403. manager->IncFeedbackConnection(port_src, port_dst);
  404. } else {
  405. manager->IncDirectConnection(port_src, port_dst);
  406. }
  407. end:
  408. WriteNextStateStop();
  409. if (res < 0)
  410. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_dst, port_src);
  411. return res;
  412. }
  413. // Server
  414. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  415. {
  416. JackConnectionManager* manager = WriteNextStateStart();
  417. JackLog("JackGraphManager::Disconnect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  418. bool in_use_src = GetPort(port_src)->fInUse;
  419. bool in_use_dst = GetPort(port_src)->fInUse;
  420. int res = 0;
  421. if (!in_use_src || !in_use_dst) {
  422. if (!in_use_src)
  423. jack_error("JackGraphManager::Disconnect: port_src not %ld used name = %s", port_src, GetPort(port_src)->fName);
  424. if (!in_use_dst)
  425. jack_error("JackGraphManager::Disconnect: port_src not %ld used name = %s", port_dst, GetPort(port_dst)->fName);
  426. res = -1;
  427. goto end;
  428. }
  429. if (!manager->IsConnected(port_src, port_dst)) {
  430. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  431. res = -1;
  432. goto end;
  433. }
  434. manager->Disconnect(port_src, port_dst);
  435. if (res < 0) {
  436. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  437. goto end;
  438. }
  439. manager->Disconnect(port_dst, port_src);
  440. if (res < 0) {
  441. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_dst, port_src);
  442. goto end;
  443. }
  444. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  445. JackLog("JackGraphManager::Disconnect: FEEDBACK removed\n");
  446. manager->DecFeedbackConnection(port_src, port_dst);
  447. } else {
  448. manager->DecDirectConnection(port_src, port_dst);
  449. }
  450. end:
  451. WriteNextStateStop();
  452. return res;
  453. }
  454. // Client
  455. int JackGraphManager::ConnectedTo(jack_port_id_t port_src, const char* port_name)
  456. {
  457. JackLog("JackGraphManager::ConnectedTo port_src = %ld port_name = %s\n", port_src, port_name);
  458. JackConnectionManager* manager = ReadCurrentState();
  459. jack_port_id_t port_dst;
  460. if ((port_dst = GetPort(port_name)) == NO_PORT) {
  461. jack_error("Unknown destination port port_name = %s", port_name);
  462. return 0;
  463. } else {
  464. return manager->IsConnected(port_src, port_dst);
  465. }
  466. }
  467. // Server
  468. int JackGraphManager::CheckPort(jack_port_id_t port_index)
  469. {
  470. JackPort* port = GetPort(port_index);
  471. if (port->fLocked) {
  472. jack_error("Port %s is locked against connection changes", port->fName);
  473. return -1;
  474. } else {
  475. return 0;
  476. }
  477. }
  478. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  479. {
  480. JackPort* src = GetPort(port_src);
  481. JackPort* dst = GetPort(port_dst);
  482. if ((dst->Flags() & JackPortIsInput) == 0) {
  483. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  484. return -1;
  485. }
  486. if ((src->Flags() & JackPortIsOutput) == 0) {
  487. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  488. return -1;
  489. }
  490. if (src->fLocked) {
  491. jack_error("Source port %s is locked against connection changes", src->fName);
  492. return -1;
  493. }
  494. if (dst->fLocked) {
  495. jack_error("Destination port %s is locked against connection changes", dst->fName);
  496. return -1;
  497. }
  498. return 0;
  499. }
  500. int JackGraphManager::CheckPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  501. {
  502. JackLog("JackGraphManager::CheckConnect src_name = %s dst_name = %s\n", src_name, dst_name);
  503. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  504. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  505. return -1;
  506. }
  507. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  508. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  509. return -1;
  510. }
  511. return CheckPorts(*port_src, *port_dst);
  512. }
  513. // Client : port array
  514. jack_port_id_t JackGraphManager::GetPort(const char* name)
  515. {
  516. for (int i = 0; i < PORT_NUM; i++) {
  517. JackPort* port = GetPort(i);
  518. if (port->IsUsed() && strcmp(port->fName, name) == 0)
  519. return i;
  520. }
  521. return NO_PORT;
  522. }
  523. /*!
  524. \brief Get the connection port name array.
  525. */
  526. // Client
  527. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  528. {
  529. const jack_int_t* connections = manager->GetConnections(port_index);
  530. jack_int_t index;
  531. int i;
  532. for (i = 0; (i < CONNECTION_NUM) && ((index = connections[i]) != EMPTY) ; i++) {
  533. JackPort* port = GetPort(index);
  534. res[i] = port->fName;
  535. }
  536. res[i] = NULL;
  537. }
  538. // Client
  539. /*
  540. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  541. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  542. read to loop forever.
  543. */
  544. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  545. {
  546. const char** res = (const char**)malloc(sizeof(char*) * (CONNECTION_NUM + 1));
  547. UInt16 cur_index;
  548. UInt16 next_index;
  549. AssertPort(port_index);
  550. do {
  551. cur_index = GetCurrentIndex();
  552. GetConnectionsAux(ReadCurrentState(), res, port_index);
  553. next_index = GetCurrentIndex();
  554. } while (cur_index != next_index); // Until a coherent state has been read
  555. return res;
  556. }
  557. // Client
  558. const char** JackGraphManager::GetPortsAux(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  559. {
  560. const char** matching_ports;
  561. unsigned long match_cnt = 0;
  562. regex_t port_regex;
  563. regex_t type_regex;
  564. bool matching;
  565. if (port_name_pattern && port_name_pattern[0]) {
  566. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  567. }
  568. if (type_name_pattern && type_name_pattern[0]) {
  569. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  570. }
  571. matching_ports = (const char**)malloc(sizeof(char*) * PORT_NUM);
  572. for (int i = 0; i < PORT_NUM; i++) {
  573. matching = true;
  574. JackPort* port = GetPort(i);
  575. if (port->IsUsed()) {
  576. if (flags) {
  577. if ((port->fFlags & flags) != flags) {
  578. matching = false;
  579. }
  580. }
  581. if (matching && port_name_pattern && port_name_pattern[0]) {
  582. if (regexec(&port_regex, port->fName, 0, NULL, 0)) {
  583. matching = false;
  584. }
  585. }
  586. /*
  587. if (matching && type_name_pattern && type_name_pattern[0]) {
  588. jack_port_type_id_t ptid = psp[i].ptype_id;
  589. if (regexec (&type_regex,engine->port_types[ptid].type_name,0, NULL, 0)) {
  590. matching = false;
  591. }
  592. }
  593. */
  594. // TO BE IMPROVED
  595. if (matching && type_name_pattern && type_name_pattern[0]) {
  596. if (regexec(&type_regex, JACK_DEFAULT_AUDIO_TYPE, 0, NULL, 0)) {
  597. matching = false;
  598. }
  599. }
  600. if (matching) {
  601. matching_ports[match_cnt++] = port->fName;
  602. }
  603. }
  604. }
  605. matching_ports[match_cnt] = 0;
  606. if (match_cnt == 0) {
  607. free(matching_ports);
  608. matching_ports = NULL;
  609. }
  610. return matching_ports;
  611. }
  612. // Client
  613. /*
  614. Check that the state was not changed during the read operation.
  615. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  616. read to loop forever.
  617. */
  618. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  619. {
  620. const char** matching_ports = NULL;
  621. UInt16 cur_index;
  622. UInt16 next_index;
  623. do {
  624. cur_index = GetCurrentIndex();
  625. if (matching_ports) {
  626. free(matching_ports);
  627. JackLog("JackGraphManager::GetPorts retry... \n");
  628. }
  629. matching_ports = GetPortsAux(port_name_pattern, type_name_pattern, flags);
  630. next_index = GetCurrentIndex();
  631. } while (cur_index != next_index); // Until a coherent state has been read
  632. return matching_ports;
  633. }
  634. // Server
  635. void JackGraphManager::Save(JackConnectionManager* dst)
  636. {
  637. JackConnectionManager* manager = WriteNextStateStart();
  638. memcpy(dst, manager, sizeof(JackConnectionManager));
  639. WriteNextStateStop();
  640. }
  641. // Server
  642. void JackGraphManager::Restore(JackConnectionManager* src)
  643. {
  644. JackConnectionManager* manager = WriteNextStateStart();
  645. memcpy(manager, src, sizeof(JackConnectionManager));
  646. WriteNextStateStop();
  647. }
  648. } // end of namespace