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.

812 lines
25KB

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