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.

808 lines
25KB

  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. /*
  265. void JackGraphManager::ReleasePort(jack_port_id_t port_index)
  266. {
  267. JackPort* port = GetPort(port_index);
  268. port->Release();
  269. }
  270. */
  271. // Server
  272. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  273. {
  274. JackConnectionManager* manager = WriteNextStateStart();
  275. JackPort* port = GetPort(port_index);
  276. int res;
  277. if (port->fFlags & JackPortIsOutput) {
  278. DisconnectAllOutput(port_index);
  279. res = manager->RemoveOutputPort(refnum, port_index);
  280. } else {
  281. DisconnectAllInput(port_index);
  282. res = manager->RemoveInputPort(refnum, port_index);
  283. }
  284. port->Release();
  285. WriteNextStateStop();
  286. return res;
  287. }
  288. // Server
  289. void JackGraphManager::RemoveAllPorts(int refnum)
  290. {
  291. JackLog("JackGraphManager::RemoveAllPorts ref = %ld\n", refnum);
  292. JackConnectionManager* manager = WriteNextStateStart();
  293. jack_port_id_t port_index;
  294. // Warning : RemovePort shift port to left, thus we always remove the first port until the "input" table is empty
  295. const jack_int_t* input = manager->GetInputPorts(refnum);
  296. while ((port_index = input[0]) != EMPTY) {
  297. ReleasePort(refnum, port_index);
  298. }
  299. // Warning : RemovePort shift port to left, thus we always remove the first port until the "output" table is empty
  300. const jack_int_t* output = manager->GetOutputPorts(refnum);
  301. while ((port_index = output[0]) != EMPTY) {
  302. ReleasePort(refnum, port_index);
  303. }
  304. WriteNextStateStop();
  305. }
  306. // Server
  307. void JackGraphManager::DisconnectAllPorts(int refnum)
  308. {
  309. int i;
  310. JackLog("JackGraphManager::DisconnectAllPorts ref = %ld\n", refnum);
  311. JackConnectionManager* manager = WriteNextStateStart();
  312. const jack_int_t* input = manager->GetInputPorts(refnum);
  313. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  314. DisconnectAllInput(input[i]);
  315. }
  316. const jack_int_t* output = manager->GetOutputPorts(refnum);
  317. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  318. DisconnectAllOutput(output[i]);
  319. }
  320. WriteNextStateStop();
  321. }
  322. // Server
  323. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  324. {
  325. JackLog("JackGraphManager::DisconnectAllInput port_index = %ld \n", port_index);
  326. JackConnectionManager* manager = WriteNextStateStart();
  327. for (int i = 0; i < PORT_NUM; i++) {
  328. if (manager->IsConnected(i, port_index)) {
  329. JackLog("JackGraphManager::Disconnect i = %ld port_index = %ld\n", i, port_index);
  330. Disconnect(i, port_index);
  331. }
  332. }
  333. WriteNextStateStop();
  334. }
  335. // Server
  336. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  337. {
  338. JackLog("JackGraphManager::DisconnectAllOutput port_index = %ld \n", port_index);
  339. JackConnectionManager* manager = WriteNextStateStart();
  340. const jack_int_t* connections = manager->GetConnections(port_index);
  341. while (connections[0] != EMPTY) {
  342. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  343. }
  344. WriteNextStateStop();
  345. }
  346. // Server
  347. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  348. {
  349. AssertPort(port_index);
  350. JackPort* port = GetPort(port_index);
  351. if (port->fFlags & JackPortIsOutput) {
  352. DisconnectAllOutput(port_index);
  353. } else {
  354. DisconnectAllInput(port_index);
  355. }
  356. return 0;
  357. }
  358. // Server
  359. void JackGraphManager::Activate(int refnum)
  360. {
  361. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  362. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  363. }
  364. /*
  365. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  366. (thus unactivated) state may happen where the client is still checked for its end.
  367. */
  368. // Server
  369. void JackGraphManager::Deactivate(int refnum)
  370. {
  371. DisconnectAllPorts(refnum);
  372. // Disconnect only when needed
  373. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  374. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  375. } else {
  376. JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
  377. }
  378. // Disconnect only when needed
  379. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  380. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  381. } else {
  382. JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
  383. }
  384. }
  385. // Server
  386. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  387. {
  388. AssertPort(port_index);
  389. JackConnectionManager* manager = WriteNextStateStart();
  390. int res = manager->GetInputRefNum(port_index);
  391. WriteNextStateStop();
  392. return res;
  393. }
  394. // Server
  395. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  396. {
  397. AssertPort(port_index);
  398. JackConnectionManager* manager = WriteNextStateStart();
  399. int res = manager->GetOutputRefNum(port_index);
  400. WriteNextStateStop();
  401. return res;
  402. }
  403. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  404. {
  405. JackConnectionManager* manager = WriteNextStateStart();
  406. JackLog("JackGraphManager::Connect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  407. bool in_use_src = GetPort(port_src)->fInUse;
  408. bool in_use_dst = GetPort(port_dst)->fInUse;
  409. int res = 0;
  410. if (!in_use_src || !in_use_dst) {
  411. if (!in_use_src)
  412. jack_error("JackGraphManager::Connect: port_src not %ld used name = %s", port_src, GetPort(port_src)->fName);
  413. if (!in_use_dst)
  414. jack_error("JackGraphManager::Connect: port_dst not %ld used name = %s", port_dst, GetPort(port_dst)->fName);
  415. res = -1;
  416. goto end;
  417. }
  418. if (manager->IsConnected(port_src, port_dst)) {
  419. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  420. res = EEXIST;
  421. goto end;
  422. }
  423. res = manager->Connect(port_src, port_dst);
  424. if (res < 0) {
  425. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  426. goto end;
  427. }
  428. manager->Connect(port_dst, port_src);
  429. if (res < 0) {
  430. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  431. goto end;
  432. }
  433. if (manager->IsLoopPath(port_src, port_dst)) {
  434. JackLog("JackGraphManager::Connect: LOOP detected\n");
  435. manager->IncFeedbackConnection(port_src, port_dst);
  436. } else {
  437. manager->IncDirectConnection(port_src, port_dst);
  438. }
  439. end:
  440. WriteNextStateStop();
  441. return res;
  442. }
  443. // Server
  444. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  445. {
  446. JackConnectionManager* manager = WriteNextStateStart();
  447. JackLog("JackGraphManager::Disconnect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  448. bool in_use_src = GetPort(port_src)->fInUse;
  449. bool in_use_dst = GetPort(port_dst)->fInUse;
  450. int res = 0;
  451. if (!in_use_src || !in_use_dst) {
  452. if (!in_use_src)
  453. jack_error("JackGraphManager::Disconnect: port_src not %ld used name = %s", port_src, GetPort(port_src)->fName);
  454. if (!in_use_dst)
  455. jack_error("JackGraphManager::Disconnect: port_src not %ld used name = %s", port_dst, GetPort(port_dst)->fName);
  456. res = -1;
  457. goto end;
  458. }
  459. if (!manager->IsConnected(port_src, port_dst)) {
  460. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  461. res = -1;
  462. goto end;
  463. }
  464. manager->Disconnect(port_src, port_dst);
  465. if (res < 0) {
  466. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  467. goto end;
  468. }
  469. manager->Disconnect(port_dst, port_src);
  470. if (res < 0) {
  471. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  472. goto end;
  473. }
  474. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  475. JackLog("JackGraphManager::Disconnect: FEEDBACK removed\n");
  476. manager->DecFeedbackConnection(port_src, port_dst);
  477. } else {
  478. manager->DecDirectConnection(port_src, port_dst);
  479. }
  480. end:
  481. WriteNextStateStop();
  482. return res;
  483. }
  484. // Client
  485. int JackGraphManager::ConnectedTo(jack_port_id_t port_src, const char* port_name)
  486. {
  487. JackLog("JackGraphManager::ConnectedTo port_src = %ld port_name = %s\n", port_src, port_name);
  488. JackConnectionManager* manager = ReadCurrentState();
  489. jack_port_id_t port_dst;
  490. if ((port_dst = GetPort(port_name)) == NO_PORT) {
  491. jack_error("Unknown destination port port_name = %s", port_name);
  492. return 0;
  493. } else {
  494. return manager->IsConnected(port_src, port_dst);
  495. }
  496. }
  497. // Server
  498. int JackGraphManager::CheckPort(jack_port_id_t port_index)
  499. {
  500. JackPort* port = GetPort(port_index);
  501. if (port->fLocked) {
  502. jack_error("Port %s is locked against connection changes", port->fName);
  503. return -1;
  504. } else {
  505. return 0;
  506. }
  507. }
  508. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  509. {
  510. JackPort* src = GetPort(port_src);
  511. JackPort* dst = GetPort(port_dst);
  512. if ((dst->Flags() & JackPortIsInput) == 0) {
  513. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  514. return -1;
  515. }
  516. if ((src->Flags() & JackPortIsOutput) == 0) {
  517. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  518. return -1;
  519. }
  520. if (src->fLocked) {
  521. jack_error("Source port %s is locked against connection changes", src->fName);
  522. return -1;
  523. }
  524. if (dst->fLocked) {
  525. jack_error("Destination port %s is locked against connection changes", dst->fName);
  526. return -1;
  527. }
  528. return 0;
  529. }
  530. int JackGraphManager::CheckPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  531. {
  532. JackLog("JackGraphManager::CheckConnect src_name = %s dst_name = %s\n", src_name, dst_name);
  533. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  534. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  535. return -1;
  536. }
  537. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  538. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  539. return -1;
  540. }
  541. return CheckPorts(*port_src, *port_dst);
  542. }
  543. // Client : port array
  544. jack_port_id_t JackGraphManager::GetPort(const char* name)
  545. {
  546. for (int i = 0; i < PORT_NUM; i++) {
  547. JackPort* port = GetPort(i);
  548. if (port->IsUsed() && strcmp(port->fName, name) == 0)
  549. return i;
  550. }
  551. return NO_PORT;
  552. }
  553. /*!
  554. \brief Get the connection port name array.
  555. */
  556. // Client
  557. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  558. {
  559. const jack_int_t* connections = manager->GetConnections(port_index);
  560. jack_int_t index;
  561. int i;
  562. for (i = 0; (i < CONNECTION_NUM) && ((index = connections[i]) != EMPTY) ; i++) {
  563. JackPort* port = GetPort(index);
  564. res[i] = port->fName;
  565. }
  566. res[i] = NULL;
  567. }
  568. /*
  569. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  570. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  571. read to loop forever.
  572. */
  573. // Client
  574. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  575. {
  576. const char** res = (const char**)malloc(sizeof(char*) * (CONNECTION_NUM + 1));
  577. UInt16 cur_index;
  578. UInt16 next_index;
  579. AssertPort(port_index);
  580. do {
  581. cur_index = GetCurrentIndex();
  582. GetConnectionsAux(ReadCurrentState(), res, port_index);
  583. next_index = GetCurrentIndex();
  584. } while (cur_index != next_index); // Until a coherent state has been read
  585. return res;
  586. }
  587. // Client
  588. const char** JackGraphManager::GetPortsAux(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  589. {
  590. const char** matching_ports;
  591. unsigned long match_cnt = 0;
  592. regex_t port_regex;
  593. regex_t type_regex;
  594. bool matching;
  595. if (port_name_pattern && port_name_pattern[0]) {
  596. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  597. }
  598. if (type_name_pattern && type_name_pattern[0]) {
  599. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  600. }
  601. matching_ports = (const char**)malloc(sizeof(char*) * PORT_NUM);
  602. for (int i = 0; i < PORT_NUM; i++) {
  603. matching = true;
  604. JackPort* port = GetPort(i);
  605. if (port->IsUsed()) {
  606. if (flags) {
  607. if ((port->fFlags & flags) != flags) {
  608. matching = false;
  609. }
  610. }
  611. if (matching && port_name_pattern && port_name_pattern[0]) {
  612. if (regexec(&port_regex, port->fName, 0, NULL, 0)) {
  613. matching = false;
  614. }
  615. }
  616. /*
  617. if (matching && type_name_pattern && type_name_pattern[0]) {
  618. jack_port_type_id_t ptid = psp[i].ptype_id;
  619. if (regexec (&type_regex,engine->port_types[ptid].type_name,0, NULL, 0)) {
  620. matching = false;
  621. }
  622. }
  623. */
  624. // TO BE IMPROVED
  625. if (matching && type_name_pattern && type_name_pattern[0]) {
  626. if (regexec(&type_regex, JACK_DEFAULT_AUDIO_TYPE, 0, NULL, 0)) {
  627. matching = false;
  628. }
  629. }
  630. if (matching) {
  631. matching_ports[match_cnt++] = port->fName;
  632. }
  633. }
  634. }
  635. matching_ports[match_cnt] = 0;
  636. if (match_cnt == 0) {
  637. free(matching_ports);
  638. matching_ports = NULL;
  639. }
  640. return matching_ports;
  641. }
  642. // Client
  643. /*
  644. Check that the state was not changed during the read operation.
  645. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  646. read to loop forever.
  647. */
  648. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  649. {
  650. const char** matching_ports = NULL;
  651. UInt16 cur_index;
  652. UInt16 next_index;
  653. do {
  654. cur_index = GetCurrentIndex();
  655. if (matching_ports) {
  656. free(matching_ports);
  657. JackLog("JackGraphManager::GetPorts retry... \n");
  658. }
  659. matching_ports = GetPortsAux(port_name_pattern, type_name_pattern, flags);
  660. next_index = GetCurrentIndex();
  661. } while (cur_index != next_index); // Until a coherent state has been read
  662. return matching_ports;
  663. }
  664. // Server
  665. void JackGraphManager::Save(JackConnectionManager* dst)
  666. {
  667. JackConnectionManager* manager = WriteNextStateStart();
  668. memcpy(dst, manager, sizeof(JackConnectionManager));
  669. WriteNextStateStop();
  670. }
  671. // Server
  672. void JackGraphManager::Restore(JackConnectionManager* src)
  673. {
  674. JackConnectionManager* manager = WriteNextStateStart();
  675. memcpy(manager, src, sizeof(JackConnectionManager));
  676. WriteNextStateStop();
  677. }
  678. } // end of namespace