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.

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