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.

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