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.

797 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. // Server
  167. int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
  168. {
  169. AssertPort(port_index);
  170. JackPort* port = GetPort(port_index);
  171. /**
  172. jackd.h
  173. * If @ref JackPortCanMonitor is set for this @a port, turn input
  174. * monitoring on or off. Otherwise, do nothing.
  175. if (!(fFlags & JackPortCanMonitor))
  176. return -1;
  177. */
  178. port->RequestMonitor(onoff);
  179. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  180. if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags & JackPortIsInput) ?
  181. jack_port_id_t src_index;
  182. for (int i = 0; (i < CONNECTION_NUM) && ((src_index = connections[i]) != EMPTY); i++) {
  183. // XXX much worse things will happen if there is a feedback loop !!!
  184. RequestMonitor(src_index, onoff);
  185. }
  186. }
  187. return 0;
  188. }
  189. // Server
  190. jack_nframes_t JackGraphManager::GetTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
  191. {
  192. const jack_int_t* connections = manager->GetConnections(port_index);
  193. jack_nframes_t latency = GetPort(port_index)->GetLatency();
  194. jack_nframes_t max_latency = 0;
  195. jack_port_id_t dst_index;
  196. if (hop_count > 8)
  197. return latency;
  198. for (int i = 0; (i < CONNECTION_NUM) && ((dst_index = connections[i]) != EMPTY); i++) {
  199. if (src_port_index != dst_index) {
  200. AssertPort(dst_index);
  201. JackPort* dst_port = GetPort(dst_index);
  202. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  203. ? dst_port->GetLatency()
  204. : GetTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  205. max_latency = MAX(max_latency, this_latency);
  206. }
  207. }
  208. return max_latency + latency;
  209. }
  210. // Server
  211. jack_nframes_t JackGraphManager::GetTotalLatency(jack_port_id_t port_index)
  212. {
  213. UInt16 cur_index;
  214. UInt16 next_index;
  215. jack_nframes_t total_latency;
  216. AssertPort(port_index);
  217. JackLog("JackGraphManager::GetTotalLatency port_index = %ld\n", port_index);
  218. do {
  219. cur_index = GetCurrentIndex();
  220. total_latency = GetTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  221. next_index = GetCurrentIndex();
  222. } while (cur_index != next_index); // Until a coherent state has been read
  223. return total_latency;
  224. }
  225. // Server
  226. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, JackPortFlags flags)
  227. {
  228. jack_port_id_t port_index;
  229. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  230. for (port_index = FIRST_AVAILABLE_PORT; port_index < PORT_NUM; port_index++) {
  231. JackPort* port = GetPort(port_index);
  232. if (!port->IsUsed()) {
  233. JackLog("JackGraphManager::AllocatePortAux port_index = %ld name = %s\n", port_index, port_name);
  234. port->Allocate(refnum, port_name, flags);
  235. break;
  236. }
  237. }
  238. return (port_index < PORT_NUM) ? port_index : NO_PORT;
  239. }
  240. // Server
  241. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, JackPortFlags flags)
  242. {
  243. JackConnectionManager* manager = WriteNextStateStart();
  244. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, flags);
  245. if (port_index != NO_PORT) {
  246. int res;
  247. if (flags & JackPortIsOutput) {
  248. res = manager->AddOutputPort(refnum, port_index);
  249. } else {
  250. res = manager->AddInputPort(refnum, port_index);
  251. }
  252. // Insertion failure
  253. if (res < 0) {
  254. JackPort* port = GetPort(port_index);
  255. assert(port);
  256. port->Release();
  257. port_index = NO_PORT;
  258. }
  259. }
  260. WriteNextStateStop();
  261. return port_index;
  262. }
  263. // Server
  264. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  265. {
  266. JackConnectionManager* manager = WriteNextStateStart();
  267. JackPort* port = GetPort(port_index);
  268. int res;
  269. if (port->fFlags & JackPortIsOutput) {
  270. DisconnectAllOutput(port_index);
  271. res = manager->RemoveOutputPort(refnum, port_index);
  272. } else {
  273. DisconnectAllInput(port_index);
  274. res = manager->RemoveInputPort(refnum, port_index);
  275. }
  276. port->Release();
  277. WriteNextStateStop();
  278. return res;
  279. }
  280. // Server
  281. void JackGraphManager::RemoveAllPorts(int refnum)
  282. {
  283. JackLog("JackGraphManager::RemoveAllPorts ref = %ld\n", refnum);
  284. JackConnectionManager* manager = WriteNextStateStart();
  285. jack_port_id_t port_index;
  286. // Warning : RemovePort shift port to left, thus we always remove the first port until the "input" table is empty
  287. const jack_int_t* input = manager->GetInputPorts(refnum);
  288. while ((port_index = input[0]) != EMPTY) {
  289. ReleasePort(refnum, port_index);
  290. }
  291. // Warning : RemovePort shift port to left, thus we always remove the first port until the "output" table is empty
  292. const jack_int_t* output = manager->GetOutputPorts(refnum);
  293. while ((port_index = output[0]) != EMPTY) {
  294. ReleasePort(refnum, port_index);
  295. }
  296. WriteNextStateStop();
  297. }
  298. // Server
  299. void JackGraphManager::DisconnectAllPorts(int refnum)
  300. {
  301. int i;
  302. JackLog("JackGraphManager::DisconnectAllPorts ref = %ld\n", refnum);
  303. JackConnectionManager* manager = WriteNextStateStart();
  304. const jack_int_t* input = manager->GetInputPorts(refnum);
  305. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  306. DisconnectAllInput(input[i]);
  307. }
  308. const jack_int_t* output = manager->GetOutputPorts(refnum);
  309. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  310. DisconnectAllOutput(output[i]);
  311. }
  312. WriteNextStateStop();
  313. }
  314. // Server
  315. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  316. {
  317. JackLog("JackGraphManager::DisconnectAllInput port_index = %ld \n", port_index);
  318. JackConnectionManager* manager = WriteNextStateStart();
  319. for (int i = 0; i < PORT_NUM; i++) {
  320. if (manager->IsConnected(i, port_index)) {
  321. JackLog("JackGraphManager::Disconnect i = %ld port_index = %ld\n", i, port_index);
  322. Disconnect(i, port_index);
  323. }
  324. }
  325. WriteNextStateStop();
  326. }
  327. // Server
  328. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  329. {
  330. JackLog("JackGraphManager::DisconnectAllOutput port_index = %ld \n", port_index);
  331. JackConnectionManager* manager = WriteNextStateStart();
  332. const jack_int_t* connections = manager->GetConnections(port_index);
  333. while (connections[0] != EMPTY) {
  334. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  335. }
  336. WriteNextStateStop();
  337. }
  338. // Server
  339. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  340. {
  341. AssertPort(port_index);
  342. JackPort* port = GetPort(port_index);
  343. if (port->fFlags & JackPortIsOutput) {
  344. DisconnectAllOutput(port_index);
  345. } else {
  346. DisconnectAllInput(port_index);
  347. }
  348. return 0;
  349. }
  350. // Server
  351. void JackGraphManager::Activate(int refnum)
  352. {
  353. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  354. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  355. }
  356. /*
  357. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  358. (thus unactivated) state may happen where the client is still checked for its end.
  359. */
  360. // Server
  361. void JackGraphManager::Deactivate(int refnum)
  362. {
  363. DisconnectAllPorts(refnum);
  364. // Disconnect only when needed
  365. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  366. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  367. } else {
  368. JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
  369. }
  370. // Disconnect only when needed
  371. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  372. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  373. } else {
  374. JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
  375. }
  376. }
  377. // Server
  378. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  379. {
  380. AssertPort(port_index);
  381. JackConnectionManager* manager = WriteNextStateStart();
  382. int res = manager->GetInputRefNum(port_index);
  383. WriteNextStateStop();
  384. return res;
  385. }
  386. // Server
  387. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  388. {
  389. AssertPort(port_index);
  390. JackConnectionManager* manager = WriteNextStateStart();
  391. int res = manager->GetOutputRefNum(port_index);
  392. WriteNextStateStop();
  393. return res;
  394. }
  395. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  396. {
  397. JackConnectionManager* manager = WriteNextStateStart();
  398. JackLog("JackGraphManager::Connect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  399. bool in_use_src = GetPort(port_src)->fInUse;
  400. bool in_use_dst = GetPort(port_dst)->fInUse;
  401. int res = 0;
  402. if (!in_use_src || !in_use_dst) {
  403. if (!in_use_src)
  404. jack_error("JackGraphManager::Connect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  405. if (!in_use_dst)
  406. jack_error("JackGraphManager::Connect: port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  407. res = -1;
  408. goto end;
  409. }
  410. if (manager->IsConnected(port_src, port_dst)) {
  411. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  412. res = EEXIST;
  413. goto end;
  414. }
  415. res = manager->Connect(port_src, port_dst);
  416. if (res < 0) {
  417. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  418. goto end;
  419. }
  420. manager->Connect(port_dst, port_src);
  421. if (res < 0) {
  422. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  423. goto end;
  424. }
  425. if (manager->IsLoopPath(port_src, port_dst)) {
  426. JackLog("JackGraphManager::Connect: LOOP detected\n");
  427. manager->IncFeedbackConnection(port_src, port_dst);
  428. } else {
  429. manager->IncDirectConnection(port_src, port_dst);
  430. }
  431. end:
  432. WriteNextStateStop();
  433. return res;
  434. }
  435. // Server
  436. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  437. {
  438. JackConnectionManager* manager = WriteNextStateStart();
  439. JackLog("JackGraphManager::Disconnect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  440. bool in_use_src = GetPort(port_src)->fInUse;
  441. bool in_use_dst = GetPort(port_dst)->fInUse;
  442. int res = 0;
  443. if (!in_use_src || !in_use_dst) {
  444. if (!in_use_src)
  445. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  446. if (!in_use_dst)
  447. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  448. res = -1;
  449. goto end;
  450. }
  451. if (!manager->IsConnected(port_src, port_dst)) {
  452. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  453. res = -1;
  454. goto end;
  455. }
  456. manager->Disconnect(port_src, port_dst);
  457. if (res < 0) {
  458. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  459. goto end;
  460. }
  461. manager->Disconnect(port_dst, port_src);
  462. if (res < 0) {
  463. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  464. goto end;
  465. }
  466. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  467. JackLog("JackGraphManager::Disconnect: FEEDBACK removed\n");
  468. manager->DecFeedbackConnection(port_src, port_dst);
  469. } else {
  470. manager->DecDirectConnection(port_src, port_dst);
  471. }
  472. end:
  473. WriteNextStateStop();
  474. return res;
  475. }
  476. // Client
  477. int JackGraphManager::ConnectedTo(jack_port_id_t port_src, const char* port_name)
  478. {
  479. JackLog("JackGraphManager::ConnectedTo port_src = %ld port_name = %s\n", port_src, port_name);
  480. JackConnectionManager* manager = ReadCurrentState();
  481. jack_port_id_t port_dst;
  482. if ((port_dst = GetPort(port_name)) == NO_PORT) {
  483. jack_error("Unknown destination port port_name = %s", port_name);
  484. return 0;
  485. } else {
  486. return manager->IsConnected(port_src, port_dst);
  487. }
  488. }
  489. // Server
  490. int JackGraphManager::CheckPort(jack_port_id_t port_index)
  491. {
  492. JackPort* port = GetPort(port_index);
  493. if (port->fLocked) {
  494. jack_error("Port %s is locked against connection changes", port->fName);
  495. return -1;
  496. } else {
  497. return 0;
  498. }
  499. }
  500. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  501. {
  502. JackPort* src = GetPort(port_src);
  503. JackPort* dst = GetPort(port_dst);
  504. if ((dst->Flags() & JackPortIsInput) == 0) {
  505. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  506. return -1;
  507. }
  508. if ((src->Flags() & JackPortIsOutput) == 0) {
  509. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  510. return -1;
  511. }
  512. if (src->fLocked) {
  513. jack_error("Source port %s is locked against connection changes", src->fName);
  514. return -1;
  515. }
  516. if (dst->fLocked) {
  517. jack_error("Destination port %s is locked against connection changes", dst->fName);
  518. return -1;
  519. }
  520. return 0;
  521. }
  522. int JackGraphManager::CheckPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  523. {
  524. JackLog("JackGraphManager::CheckConnect src_name = %s dst_name = %s\n", src_name, dst_name);
  525. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  526. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  527. return -1;
  528. }
  529. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  530. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  531. return -1;
  532. }
  533. return CheckPorts(*port_src, *port_dst);
  534. }
  535. // Client : port array
  536. jack_port_id_t JackGraphManager::GetPort(const char* name)
  537. {
  538. for (int i = 0; i < PORT_NUM; i++) {
  539. JackPort* port = GetPort(i);
  540. if (port->IsUsed() && port->NameEquals(name))
  541. return i;
  542. }
  543. return NO_PORT;
  544. }
  545. /*!
  546. \brief Get the connection port name array.
  547. */
  548. // Client
  549. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  550. {
  551. const jack_int_t* connections = manager->GetConnections(port_index);
  552. jack_int_t index;
  553. int i;
  554. for (i = 0; (i < CONNECTION_NUM) && ((index = connections[i]) != EMPTY); i++) {
  555. JackPort* port = GetPort(index);
  556. res[i] = port->fName;
  557. }
  558. res[i] = NULL;
  559. }
  560. /*
  561. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  562. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  563. read to loop forever.
  564. */
  565. // Client
  566. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  567. {
  568. const char** res = (const char**)malloc(sizeof(char*) * (CONNECTION_NUM + 1));
  569. UInt16 cur_index;
  570. UInt16 next_index;
  571. AssertPort(port_index);
  572. do {
  573. cur_index = GetCurrentIndex();
  574. GetConnectionsAux(ReadCurrentState(), res, port_index);
  575. next_index = GetCurrentIndex();
  576. } while (cur_index != next_index); // Until a coherent state has been read
  577. return res;
  578. }
  579. // Client
  580. const char** JackGraphManager::GetPortsAux(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  581. {
  582. const char** matching_ports;
  583. unsigned long match_cnt = 0;
  584. regex_t port_regex;
  585. regex_t type_regex;
  586. bool matching;
  587. if (port_name_pattern && port_name_pattern[0]) {
  588. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  589. }
  590. if (type_name_pattern && type_name_pattern[0]) {
  591. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  592. }
  593. matching_ports = (const char**)malloc(sizeof(char*) * PORT_NUM);
  594. for (int i = 0; i < PORT_NUM; i++) {
  595. matching = true;
  596. JackPort* port = GetPort(i);
  597. if (port->IsUsed()) {
  598. if (flags) {
  599. if ((port->fFlags & flags) != flags) {
  600. matching = false;
  601. }
  602. }
  603. if (matching && port_name_pattern && port_name_pattern[0]) {
  604. if (regexec(&port_regex, port->fName, 0, NULL, 0)) {
  605. matching = false;
  606. }
  607. }
  608. /*
  609. if (matching && type_name_pattern && type_name_pattern[0]) {
  610. jack_port_type_id_t ptid = psp[i].ptype_id;
  611. if (regexec (&type_regex,engine->port_types[ptid].type_name,0, NULL, 0)) {
  612. matching = false;
  613. }
  614. }
  615. */
  616. // TO BE IMPROVED
  617. if (matching && type_name_pattern && type_name_pattern[0]) {
  618. if (regexec(&type_regex, JACK_DEFAULT_AUDIO_TYPE, 0, NULL, 0)) {
  619. matching = false;
  620. }
  621. }
  622. if (matching) {
  623. matching_ports[match_cnt++] = port->fName;
  624. }
  625. }
  626. }
  627. matching_ports[match_cnt] = 0;
  628. if (match_cnt == 0) {
  629. free(matching_ports);
  630. matching_ports = NULL;
  631. }
  632. return matching_ports;
  633. }
  634. // Client
  635. /*
  636. Check that the state was not changed during the read operation.
  637. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  638. read to loop forever.
  639. */
  640. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  641. {
  642. const char** matching_ports = NULL;
  643. UInt16 cur_index;
  644. UInt16 next_index;
  645. do {
  646. cur_index = GetCurrentIndex();
  647. if (matching_ports) {
  648. free(matching_ports);
  649. JackLog("JackGraphManager::GetPorts retry... \n");
  650. }
  651. matching_ports = GetPortsAux(port_name_pattern, type_name_pattern, flags);
  652. next_index = GetCurrentIndex();
  653. } while (cur_index != next_index); // Until a coherent state has been read
  654. return matching_ports;
  655. }
  656. // Server
  657. void JackGraphManager::Save(JackConnectionManager* dst)
  658. {
  659. JackConnectionManager* manager = WriteNextStateStart();
  660. memcpy(dst, manager, sizeof(JackConnectionManager));
  661. WriteNextStateStop();
  662. }
  663. // Server
  664. void JackGraphManager::Restore(JackConnectionManager* src)
  665. {
  666. JackConnectionManager* manager = WriteNextStateStart();
  667. memcpy(manager, src, sizeof(JackConnectionManager));
  668. WriteNextStateStop();
  669. }
  670. } // end of namespace